简体   繁体   中英

Issue in sending mail using AWS SESClient yii2 with PDF attachment

We are trying to send the email to the multiple users with PDF attachment. The attached PDF is generated via kartik\\mpdf\\Pdf.

Steps we have followed

  1. Fetch users and message content from the DB
  2. For all available users generate Email object and also generate PDF and send that mail with attached pdf.
  3. To send the email we are using amazon SES.

In config file pdf component is set

'pdf' => [
    'class' => Pdf::classname(),
    'format' => Pdf::FORMAT_A4,
    'orientation' => Pdf::ORIENT_PORTRAIT,
    'destination' => Pdf::DEST_STRING,
]

===========================

Code in PHP file to send email

use Aws\Ses\SesClient;

//Fetch all records whose mail_send is 0 and in_queue is 0
$email_model = new MailQueue();
$mail_queue_data = // select query to get Email IDs, email content etc from mail queue table;

foreach($mail_queue_data as $result) 
{
    try {
        // Create SES client Instance
        $gaws_ses_client = SesClient::factory(array(
            'version' => 'latest',
            'region'  => 'us-east-1',
            'credentials' => [
                'key' => 'YOUR_KEY',
                'secret' => 'YOUR_SECRET_KEY',
            ]
        ));

        // Set SES linked user email
        $my_from_address = 'this_is_test@demo.com';

        $my_string_to_addresses = $result['to_email'];
        $my_array_to_adresses = [$my_string_to_addresses];

        $my_subject = isset($result['subject']) ? $result['subject'] : '';
        $my_html_body = isset($result['email_html']) ? $result['email_html'] : '';

        // Set mail message
        $my_message = "";
        $my_message .= "MIME-Version: 1.0\n";
        $my_message .= "To: ".$my_string_to_addresses."\n"; 
        $my_message .= "From:".$my_from_address."\n";   
        $my_message .= "Subject:".$my_subject."\n";

        $my_separator = md5(time());
        $my_separator_multipart = md5($my_subject . time());

        // Set headers
        $my_message .= "Content-Type: multipart/mixed; boundary=\"".$my_separator_multipart."\"\n";
        $my_message .= "\n--".$my_separator_multipart."\n";
        $my_message .= "Content-Type: multipart/alternative; boundary=\"".$my_separator."\"\n";
        $my_message .= "\n--".$my_separator."\n";
        $my_message .= "Content-Type: text/html; charset=\"UTF-8\"\n";
        $my_message .= "\n".$my_html_body."\n";
        $my_message .= "\n--".$my_separator."--\n";

        $content = $my_html_body;
        $pdf = Yii::$app->pdf;
        $pdf->cssFile = '@backend/web/css/externalcss_file_name.css';
        $pdf->content = $content;
        $raw_data = $pdf->render();

        $my_file_name = 'attachment.pdf';

        $my_data_attached_file = chunk_split(base64_encode($raw_data));

        $file_info = new \finfo(FILEINFO_MIME_TYPE);
        $my_file_mime_type = $file_info->buffer($raw_data);

        $my_message .= "--".$my_separator_multipart."\n";
        $my_message .= "Content-Type: ".$my_file_mime_type."; name=\"".$my_file_name."\"\n";
        $my_message .= "Content-Disposition: attachment; filename=\"".$my_file_name."\"\n";
        $my_message .= "Content-Transfer-Encoding: base64\n\n";
        $my_message .= $my_data_attached_file."\n";
        $my_message .= "--".$my_separator_multipart."--";

        $my_array_ses = [
            'Source'       => $my_from_address,
            'Destinations' => $my_array_to_adresses,
            'RawMessage'   => [
                'Data' => $my_message
            ]
        ];

        $my_api_result = $gaws_ses_client->sendRawEmail($my_array_ses);

        //view sample output
        echo "\n<BR>SES Result:<BR>\n";
        echo "Message ID : ".$my_api_result['MessageId']."<BR>\n";
        print_r($my_api_result);
        echo "<BR>\n";

    } catch (Exception $my_obj_error) {
        //An error happened and the email did not get sent
        $my_error_info = $my_obj_error->getMessage();

        echo "\n<BR>*** SES ERROR:<BR>\n";
        print_r($my_error_info);
        echo "<BR>\n";      

        $myfile = fopen("ses_send_error.txt", "w");
        fwrite($myfile, $my_error_info);
        fclose($myfile);

    }
}
  1. Now, if we are trying to send only one email then its working fine. But if we are sending email to the mutiple users then we are getting below error:

PHP Notice – yii\\base\\ErrorException

Undefined index: data

That exception generates when PDF contains images. If we generate PDF without images, it works properly. So how to resolve this issue?

在此处输入图片说明

In order to troubleshoot this, I would suggest to hardcode the to email addresses as below. Make sure to add the real email addresses. Once you get it working, then you can format the my_string_to_addresses to this format.

$my_array_to_adresses = ['email1@domain.com', 'email2@domain.com'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM