使用带有PDF附件的AWS SESClient yii2发送邮件时出现问题

时间:2019-12-02 11:06:04

标签: php yii2 amazon-ses mpdf

我们正尝试将电子邮件发送给具有PDF附件的多个用户。附件PDF是通过kartik \ mpdf \ Pdf生成的。

我们遵循的步骤

  1. 从数据库中获取用户和消息内容
  2. 为所有可用用户生成Email对象,并生成PDF并发送带有pdf附件的邮件。
  3. 要发送电子邮件,我们正在使用Amazon SES。

在配置文件中设置了pdf组件

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

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

PHP文件中的代码以发送电子邮件

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. 现在,如果我们仅尝试发送一封电子邮件,那么它可以正常工作。但是,如果我们向多个用户发送电子邮件,那么我们将遇到以下错误:
  

PHP公告– yii \ base \ ErrorException

     

未定义索引:数据

当PDF包含图像时,将生成该异常。如果我们生成没有图像的PDF,则它可以正常工作。那么如何解决这个问题呢?

enter image description here

1 个答案:

答案 0 :(得分:0)

为了解决此问题,我建议如下将硬编码到电子邮件地址。确保添加真实的电子邮件地址。一旦工作,您就可以将my_string_to_addresses格式化为这种格式。

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