亚马逊 ses 邮件附件 php

amazon ses mail attachment php

我正在尝试使用 php 中的 amazon SES sendmail() 函数在邮件中发送 pdf 附件。 我编写了一个以 MIME 类型为内容并发送邮件的函数。 但是我无法在邮件中发送附件。 文件路径和所有其他值似乎都很完美。

函数代码如下:

/*
* Function sendRawMail() is used to send mails to user with attachments
*/
public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath) 
{

    $domain = explode('@', $to);
    if (count($domain) > 1 && $domain[1] == 'guest.com') {
        $to = 'knowlensguestuser3@gmail.com';
    }

    $destination = array();
    $destination['ToAddresses'] = array($to);
    if($cc != '')
    {
        $cc = explode(',', $cc);
        $destination['CcAddresses'] = $cc;
    }
    if($bcc != '')
    {
        $bcc = explode(',', $bcc);
        $destination['BccAddresses'] = $bcc;
    }


    $replyTo = 'notifications@knowlens.com';

    $client = SesClient::factory(array(
        'key' => Yii::$app->params['aws.id'],
        'secret' => Yii::$app->params['aws.secret'],
        'region' => 'us-east-1',
    ));

    $message= "To: ".$to."\n";
    $message.= "From: ".$replyTo."\n";
    $message.= "Subject: ".$subject."\n";
    $message.= "MIME-Version: 1.0\n";
    $message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= 'Content-Type: text/plain; charset="utf-8"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: 7bit\n";
    $message.= "Content-Disposition: inline\n";
    $message.= "\n";
    $message.= $body;
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>\n";
    $message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: base64\n";
    $message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
    $message.= "\n";
    $message.= base64_encode(file_get_contents($filepath));
    $message.= "\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";

    $result = $client->SendRawEmail(array(
        // Source is required
        'Source' => '​​​​Knowlens Solutions Pvt. Ltd. <notifications@knowlens.com>',
        // Destination is required
        'Destination' => $destination,
        // Message is required
        'RawMessage' => array(
            // Data is required
            'Data' => base64_encode($message),
        ),

    ));

}

邮件已成功发送给用户,但没有附件。 请帮忙

邮件的总大小不能超过 10 MB。这包括作为邮件一部分的任何附件。 您检查过您的 pdf 文件的大小了吗?

谢谢。它对我有用。 更新代码如下:

函数 sendRawMail() 用于向用户发送邮件(带附件的 AWS 邮件)

public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath) 
{
    $precc = $cc;
    $prebcc = $bcc;

    $domain = explode('@', $to);
    if (count($domain) > 1 && $domain[1] == 'ABC.com') {
        $to = 'guestuser3@ABC.com';
    }

    $destination = array();
    $destination['ToAddresses'] = array($to);
    if($cc != '')
    {
        $cc = explode(',', $cc);
        $destination['CcAddresses'] = $cc;
    }
    if($bcc != '')
    {
        $bcc = explode(',', $bcc);
        $destination['BccAddresses'] = $bcc;
    }


    $replyTo = 'notifications@knowlens.com';

    $client = SesClient::factory(array(
        'key' => Yii::$app->params['aws.id'],
        'secret' => Yii::$app->params['aws.secret'],
        'region' => 'us-east-1',
    ));

    $message= "To: ".$to."\n";
    $message.= "From: ".$replyTo."\n";
    if($precc != '')
    {
        $message.= "Cc: ".$precc."\n";
    }
    if($prebcc != '')
    {
        $message.= "Bcc: ".$prebcc."\n";
    }
    $message.= "Subject: ".$subject."\n";
    $message.= "MIME-Version: 1.0\n";
    $message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= 'Content-Type: text/html; charset="utf-8"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: 7bit\n";
    $message.= "Content-Disposition: inline\n";
    $message.= "\n";
    $message.= $body;
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>\n";
    $message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: base64\n";
    $message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
    $message.= "\n";
    $message.= base64_encode(file_get_contents($filepath));
    $message.= "\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";
    $result = $client->SendRawEmail(array(
        // Source is required
        'Source' => 'ABC Solutions Pvt. Ltd. <notifications@ABC.com>',
        // Destination is required
        'Destination' => $destination,
        // Message is required
        'RawMessage' => array(
            // Data is required
            'Data' => base64_encode($message),
        ),

    ));
    return $result;
}