Pregunta

¿Cómo puedo adjuntar contenido dinámico de Mandrill WebHook y enviarlo usando SWIFTMAILER y SMTP (Mandrill).

Aquí está mi código:

<?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);
}
?>

Busco a través y leo a través de SwiftMailer Docs, pero no pude encontrar una manera que resuelva mi problema. ¡Todo adjunto estará corrupto en el destino!

¿Fue útil?

Solución

El contenido de los archivos adjuntos en Mandrill se basa en Base64 codificada por defecto (ver. Campo base64 en el JSON).

Antes de establecer el contenido adjunto en SWIFTMAILER, debe decodificar el contenido.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top