I am trying to send pdf attachments in mail using amazon SES sendmail() function in php . I have written a function which takes MIME type as content and send a mail. But I am not able to send attachment in the mail. the file paths and all the other values are seems perfect.
The functions code is as follows :
/*
* Function sendRawMail() is used to send mails to user with attachments
*/
public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath)
{
$domain = explode('@', $to);
if (count($domain) > 1 && $domain[1] == 'guest.com') {
$to = 'knowlensguestuser3@gmail.com';
}
$destination = array();
$destination['ToAddresses'] = array($to);
if($cc != '')
{
$cc = explode(',', $cc);
$destination['CcAddresses'] = $cc;
}
if($bcc != '')
{
$bcc = explode(',', $bcc);
$destination['BccAddresses'] = $bcc;
}
$replyTo = 'notifications@knowlens.com';
$client = SesClient::factory(array(
'key' => Yii::$app->params['aws.id'],
'secret' => Yii::$app->params['aws.secret'],
'region' => 'us-east-1',
));
$message= "To: ".$to."
";
$message.= "From: ".$replyTo."
";
$message.= "Subject: ".$subject."
";
$message.= "MIME-Version: 1.0
";
$message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number
";
$message.= 'Content-Type: text/plain; charset="utf-8"';
$message.= "
";
$message.= "Content-Transfer-Encoding: 7bit
";
$message.= "Content-Disposition: inline
";
$message.= "
";
$message.= $body;
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number
";
$message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>
";
$message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
$message.= "
";
$message.= "Content-Transfer-Encoding: base64
";
$message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
$message.= "
";
$message.= base64_encode(file_get_contents($filepath));
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number--
";
$result = $client->SendRawEmail(array(
// Source is required
'Source' => 'Knowlens Solutions Pvt. Ltd. <notifications@knowlens.com>',
// Destination is required
'Destination' => $destination,
// Message is required
'RawMessage' => array(
// Data is required
'Data' => base64_encode($message),
),
));
}
Mail is successfully sent to the user, but without attachment. Please help.
The total size of the message cannot exceed 10 MB. This includes any attachments that are part of the message. Have you checked the size of your pdf file?
Thanks. It worked for me. Updated code as follows :
Function sendRawMail() is used to send mails to user (AWS mail with attachment)
public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath)
{
$precc = $cc;
$prebcc = $bcc;
$domain = explode('@', $to);
if (count($domain) > 1 && $domain[1] == 'ABC.com') {
$to = 'guestuser3@ABC.com';
}
$destination = array();
$destination['ToAddresses'] = array($to);
if($cc != '')
{
$cc = explode(',', $cc);
$destination['CcAddresses'] = $cc;
}
if($bcc != '')
{
$bcc = explode(',', $bcc);
$destination['BccAddresses'] = $bcc;
}
$replyTo = 'notifications@knowlens.com';
$client = SesClient::factory(array(
'key' => Yii::$app->params['aws.id'],
'secret' => Yii::$app->params['aws.secret'],
'region' => 'us-east-1',
));
$message= "To: ".$to."
";
$message.= "From: ".$replyTo."
";
if($precc != '')
{
$message.= "Cc: ".$precc."
";
}
if($prebcc != '')
{
$message.= "Bcc: ".$prebcc."
";
}
$message.= "Subject: ".$subject."
";
$message.= "MIME-Version: 1.0
";
$message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number
";
$message.= 'Content-Type: text/html; charset="utf-8"';
$message.= "
";
$message.= "Content-Transfer-Encoding: 7bit
";
$message.= "Content-Disposition: inline
";
$message.= "
";
$message.= $body;
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number
";
$message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>
";
$message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
$message.= "
";
$message.= "Content-Transfer-Encoding: base64
";
$message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
$message.= "
";
$message.= base64_encode(file_get_contents($filepath));
$message.= "
";
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number--
";
$result = $client->SendRawEmail(array(
// Source is required
'Source' => 'ABC Solutions Pvt. Ltd. <notifications@ABC.com>',
// Destination is required
'Destination' => $destination,
// Message is required
'RawMessage' => array(
// Data is required
'Data' => base64_encode($message),
),
));
return $result;
}