Forward an email which decode from mandrill webhook, by php SwiftMailer
-
20-12-2019 - |
Question
I'm using mandrill to manage my website emails.But I also want to use a mailbox like yahoo or google so I set a route in mandrill which forward inbound emails that will send to info@mydomain.com, to my default mail box(myEmail@ymail.com).I wrote a PHP code which receive an email, decode it, and forward it to a my email. I use SwiftMailer to send SMTP email. It works nice for emails without any attachment.But there is a strange problem with attachments.They deliver corruptly.I can not open them. I search throughly and test a lot, but unfortunately couldn't find the problem.
<?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);
}
?>
Solution
At the end I couldn't find the problem with SwiftMailer but I could answer my need with mandrill API,
this code works for me:
<?php
if(!isset($_POST['mandrill_events'])) {
echo 'A mandrill error occurred: Invalid mandrill_events';
exit;
}
require 'mandrill/src/Mandrill.php';
// -------------- Receive --------------------------
$mail = array_pop(json_decode($_POST['mandrill_events']));
$attachments = array();
foreach ($mail->msg->attachments as $attachment) {
$attachments[] = array(
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->content,
);
}
$headers = array();
// Support only Reply-to header
if(isset($mail->msg->headers->{'Reply-to'})) {
$headers[] = array('Reply-to' => $mail->msg->headers->{'Reply-to'});
}
// ----------------- Send ----------------------------------------
try {
$mandrill = new Mandrill('-------------');
$message = array(
'html' => $mail->msg->html,
'text' => 'Example text content',
'subject' => $mail->msg->subject . " From " . $mail->msg->from_email,
'from_email' => 'info@mydomain.com',
'from_name' => 'info',
'to' => array(
array(
'email' => 'me@yahoo.com',
'name' => 'me',
'type' => 'to'
)
),
'headers' => array('Reply-To' => 'info@mydomain.com'),
'important' => false,
'track_opens' => null,
'track_clicks' => null,
'auto_text' => null,
'auto_html' => null,
'inline_css' => null,
'url_strip_qs' => null,
'preserve_recipients' => null,
'view_content_link' => null,
'bcc_address' => null,
'tracking_domain' => null,
'signing_domain' => null,
'return_path_domain' => null,
'merge' => true,
'attachments' => $attachments
);
$async = false;
$ip_pool = 'Main Pool';
$send_at = null;
$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow