亚马逊ses邮件附件php

时间:2016-05-31 07:41:20

标签: php email amazon-web-services amazon-ses

我正在尝试使用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),
        ),

    ));

}

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

函数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;
}