【问题标题】:SwiftMailer corrupt attachment with dynamic content(using mandrill)带有动态内容的 SwiftMailer 损坏附件(使用 mandrill)
【发布时间】:2014-09-20 13:21:54
【问题描述】:

如何从 mandrill webhook 附加动态内容,并使用 Swiftmailer 和 SMTP(Mandrill) 发送。

这是我的代码:

<?php   

if(!isset($_POST['mandrill_events'])) {
echo 'A mandrill error occurred: Invalid mandrill_events';
exit;
}

// -------------- Receive --------------------------
$mail = array_pop(json_decode($_POST['mandrill_events']));


// ----------------- Send ----------------------------------------
include_once "swiftmailer-master/lib/swift_required.php";

$subject = $mail->msg->subject . " From " . $mail->msg->from_email;
$from = array('info@myDomain.ir' =>'myDomain');
$to = array(
  'myEmail@yahoo.com' => 'Hamed Gh'
);

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 25,tls);
$transport->setUsername('username');
$transport->setPassword('***********');

$swift = Swift_Mailer::newInstance($transport);

$message = new Swift_Message($subject);
$message->setFrom($from);

//I think there is a problem here!!
foreach ($mail->msg->attachments as $attachment) {
$myType = $attachment->type;
$myName = $attachment->name;
$myContent = $attachment->content;

$attachment = Swift_Attachment::newInstance()
      ->setFilename($myName)
      ->setContentType($myType)
      ->setBody($myContent)
      ;
$message->attach($attachment);
}

$body = $mail->msg->html;
$message->setBody($body, 'text/html');

$message->setTo($to);

$text = "Mandrill speaks plaintext";
$message->addPart($text, 'text/plain');

if($recipients = $swift->send($message, $failures) )
{
 echo 'Message successfully sent!';
} else {
 echo "There was an error:\n";
 print_r($failures);
}
?>

我彻底搜索并阅读了 SwiftMailer 文档,但找不到解决问题的方法。所有附件在目的地都会损坏!

【问题讨论】:

    标签: php email-attachments swiftmailer mandrill


    【解决方案1】:

    Mandrill 中附加文件的内容默认为 Base64 编码(参见 Json 中的 base64 字段)。

    所以在 SwiftMailer 中设置附加内容之前,需要对内容进行解码。

    $myContent = base64_decode($attachment->content);
    

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2011-10-28
      相关资源
      最近更新 更多