I use Laravel 4 framework with AWS sdk for SES. I am able to send regular emails using sendEmail function. I want to be able to attach files to the emails, the problem is that I can't find how to do it.
is it even possible to use sendEmail function to attach files or I must use send_raw_email function? (how to do this?)
this how I use SES:
$msg['Source'] = Config::get('mail.mailSource');
$msg['Destination']['ToAddresses'][] = $_GET['email'];
$msg['Message']['Subject']['Data'] = "bla bla";
$msg['Message']['Body']['Text']['Data'] = 'bla bla';
$msg['Message']['Body']['Html']['Data'] = 'bla bla';
$ses = AWS::get('ses');
$ses->sendEmail($msg);
I looked at AWS sdk in laravel and found there array with requirements for sendEmail function but there are no clues for attach files
'SendEmail' => array(
'httpMethod' => 'POST',
'uri' => '/',
'class' => 'Aws\\Common\\Command\\QueryCommand',
'responseClass' => 'SendEmailResponse',
'responseType' => 'model',
'parameters' => array(
'Action' => array(
'static' => true,
'location' => 'aws.query',
'default' => 'SendEmail',
),
'Version' => array(......
the only way that I found to send emails with attachments (using SES SERVICE) is use SendRawEmail method.
$message = "To: ". $_GET['email'] ."
";
$message .= "From: ". $msg['Source'] ."
";
$message .= "Subject: Example SES mail (raw)
";
$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 .= "Dear new tester,
";
$message .= "Attached is the file you requested.
";
$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/zip; name="shell.zip"';
$message .= "
";
$message .= "Content-Transfer-Encoding: base64
";
$message .= 'Content-Disposition: attachment; filename="file.png"';
$message .= "
";
$message .= base64_encode( $attachedFile );
$message .= "
";
$message .= "--aRandomString_with_signs_or_9879497q8w7r8number--
";
$sendMsg['RawMessage']['Data'] = (string)base64_encode($message);
$sendMsg['RawMessage']['Source'] = $msg['Source'];
$sendMsg['RawMessage']['Destinations'] = $_GET['email'];
$ses->SendRawEmail($sendMsg);
pay attention to this rows:
$message .= 'Content-Disposition: attachment; filename="file.png"';
$message .= base64_encode( $attachedFile );
In V3.x of the AWS PHP, the base64_encode should not be used for the whole message, only for the attachment encoding. So in V3.x, do not BASE64 ENCODE $myMessage below:
$myArraySES = [
'Source' => $myFromAdress,
'Destinations' => $myArrayToAdresses,
'RawMessage' => [
'Data' => $myMessage
]
];
Here is my working solution for PHP 7 with V3.x of AWS PHP SDK. This solution will send a HTML e-mail with one attachment via AWS SES:
use Aws\Ses\SesClient;
$gAWS_SES_client = SesClient::factory(array(
'version'=> 'latest',
'region' => 'eu-west-1',
'credentials' => array(
'key' => 'my_secret_key',
'secret' => 'my_secret_pw'
)
));
function sendRAWEmailViaSES($myArrayToAdresses, $myFromAdress, $mySubject, $myHTMLBody, $myFilePath){
global $gAWS_SES_client;
echo "
<BR>Now in sendRAWEmailViaSES()<BR>
";
$myStringToAddresses = implode(",", $myArrayToAdresses);
$myFileName = basename($myFilePath);;
$myDataAttachedFile = file_get_contents($myFilePath);
$myDataAttachedFile = chunk_split(base64_encode($myDataAttachedFile));
$myFileMimeInfo = finfo_open(FILEINFO_MIME_TYPE);
$myFileMimeType = finfo_file($myFileMimeInfo, $myFilePath);
$mySeparator = md5(time());
$mySeparator_multipart = md5($mySubject . time());
$myMessage = "";
$myMessage .= "MIME-Version: 1.0
";
$myMessage .= "To: ".$myStringToAddresses."
";
$myMessage .= "From:".$myFromAdress."
";
$myMessage .= "Subject:".$mySubject."
";
$myMessage .= "Content-Type: multipart/mixed; boundary=\"".$mySeparator_multipart."\"
";
$myMessage .= "
--".$mySeparator_multipart."
";
$myMessage .= "Content-Type: multipart/alternative; boundary=\"".$mySeparator."\"
";
$myMessage .= "
--".$mySeparator."
";
$myMessage .= "Content-Type: text/html; charset=\"UTF-8\"
";
$myMessage .= "
".$myHTMLBody."
";
$myMessage .= "
--".$mySeparator."--
";
$myMessage .= "--".$mySeparator_multipart."
";
$myMessage .= "Content-Type: ".$myFileMimeType."; name=\"".$myFileName."\"
";
$myMessage .= "Content-Disposition: attachment; filename=\"".$myFileName."\"
";
$myMessage .= "Content-Transfer-Encoding: base64
";
$myMessage .= $myDataAttachedFile."
";
$myMessage .= "--".$mySeparator_multipart."--";
//echo "
<BR>Message:<BR>
";
//print_r($myMessage);
//echo "<BR>
";
$myArraySES = [
'Source' => $myFromAdress,
'Destinations' => $myArrayToAdresses,
'RawMessage' => [
'Data' => $myMessage
]
];
echo "
<HR>Trying to send mail via AWS SES <BR>
";
try{
$myResult = true;
$myAPIResult = $gAWS_SES_client->sendRawEmail($myArraySES);
//save the MessageId which can be used to track the request
//$myMessageArrayID = $myAPIResult->get('MessageId');
//echo("MessageId: $msg_id");
//view sample output
echo "
<BR>SES Result:<BR>
";
print_r($myAPIResult);
echo "<BR>
";
} catch (Exception $myObjError) {
//An error happened and the email did not get sent
$myErrorInfo = $myObjError->getMessage();
echo "
<BR>*** SES ERROR:<BR>
";
print_r($myErrorInfo);
echo "<BR>
";
$myfile = fopen("ses_send_error.txt", "w");
fwrite($myfile, $myErrorInfo);
fclose($myfile);
}
}