SwiftMailerダイナミックコンテンツを使用した著作権損傷(マンドリルを使用)

StackOverflow https://stackoverflow.com//questions/25010766

質問

Mandrill Webhookから動的コンテンツを添付し、SwiftMailerとSMTP(マンドリル)を使用して送信する方法。

これは私のコードです:

<?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のドキュメントを検索しますが、私の問題を解決する方法を見つけることができませんでした。

役に立ちましたか?

解決

マンドリル内の添付ファイルの内容は、デフォルトで符号化されたBase64です(JSONのBase64フィールド)。

SO SWIFTMailerで添付されたコンテンツを設定する前に、コンテンツをデコードする必要があります。

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top