문제

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 문서를 읽고 읽으려고하지만 내 문제를 해결하는 방법을 찾을 수 없었습니다. 모든 첨부 파일은 목적지에서 손상됩니다!

도움이 되었습니까?

해결책

맨드 릴의 첨부 파일의 내용은 기본적으로 Base64 인코딩됩니다 (JSON의 Base64 필드 참조).

Swiftmailer에서 첨부 된 콘텐츠를 설정하기 전에 콘텐츠를 디코딩해야합니다.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top