当前位置: 首页 > 知识库问答 >
问题:

亚马逊ses邮件附件php

邹普松
2023-03-14

我正在尝试使用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个答案

邹祺
2023-03-14

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

函数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;
}
邵奇
2023-03-14

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

 类似资料:
  • 我有一个Symfony2应用程序,需要使用AmazonSES发送邮件。我需要使用SMTP进行设置。由于某种原因,我的Symfony2应用程序自从我在实例上运行以来一直无法发送邮件。它在我的本地服务器上运行良好,使用gmail发送邮件,但在实时服务器上niether gmail不起作用。我的配置是: 我已经从SES控制台在线获取了SMTPHOST、SMTPUSER和SMTPPASS这三件东西。但邮件

  • 我试图使用与这里完全相同的代码:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-java.html我更改了。但是我不能发送电子邮件。这两封电子邮件都已添加并验证了Amazon SES。 我正在使用邮件依赖项: 日志: 问题在哪里?

  • 我正在尝试使用AmazonSDK发送电子邮件。NET和SES。我有一封由特殊信件组成的电子邮件,例如: ęxąmplę@źćż。通用域名格式 对于域部分,我阅读了关于Punycode的文章,这很好用。但对于地址的本地部分,我似乎找不到解决方案:我尝试对整个电子邮件使用RFC 2047编码,但SES返回“missing final@domain”错误,因此我尝试只对本地部分进行编码,因此电子邮件将是

  • 我试图通过亚马逊SES设置一个电子邮件队列,以确保我可以同时发送多封电子邮件(每秒限制为14封电子邮件)。 我总是收到错误的信号 电子邮件未发送。错误消息:在上执行“发送电子邮件”时出错https://email.us-west-2.amazonaws.com"; AWS HTTP错误:客户端错误:导致了一个响应:发件人签名o(截断…)签名desnotmatch(客户端):我们计算的请求签名与您提

  • 我正在尝试使用Amazon SES发送电子邮件。我在地区工作过,刚刚把我的地区切换到,我无法向我的个人gmail账户发送电子邮件。我已经从地址验证了的

  • 我已经在我的服务器上配置了postfix,只发送@gmail。com邮件至亚马逊SES: 此外,我在AmazonSES控制台中配置了使用AmazonSNS接收邮件的反弹和投诉。问题是,如果我向一个不存在的gmail地址发送邮件,我不会收到回音。 如果从邮件发送邮件。谷歌。com到地址dsadaerwer。拉拉乐队很烂。贾斯汀是一个-beaver@gmail.com我收到: 但是如果从PHP脚本发送