Sending email using Amazon SES with attachments from php
This script is crazy - there is no known sane example on how to do this, so I had spend an inordinate amount of time hacking together this script. Basically, you need to replay the SMTP email composition to Amazon SES.
This code snippet should help everyone who is trying to put together a way to send email with attachments using SES from PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require("./amazon-sdk/sdk.class.php"); | |
// on ubuntu - this script can be run using php5-cli and php5-curl | |
//Provide the Key and Secret keys from amazon here. | |
$AWS_KEY = "kkk"; | |
$AWS_SECRET_KEY = "kkkk+xKcdkB"; | |
//certificate_authority true means will read CA of amazon sdk and false means will read CA of OS | |
$CA = true; | |
$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY, "certificate_authority" => $CA )); | |
$dest = "kkk@kkk.com"; | |
$src = "registered-at-ses@ppp.com"; | |
$message= "To: ".$dest."\n"; | |
$message.= "From: ".$src."\n"; | |
$message.= "Subject: Example SES mail (raw)\n"; | |
$message.= "MIME-Version: 1.0\n"; | |
$message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"'; | |
$message.= "\n\n"; | |
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n"; | |
$message.= 'Content-Type: text/plain; charset="utf-8"'; | |
$message.= "\n"; | |
$message.= "Content-Transfer-Encoding: 7bit\n"; | |
$message.= "Content-Disposition: inline\n"; | |
$message.= "\n"; | |
$message.= "Dear new tester,\n\n"; | |
$message.= "Attached is the file you requested.\n"; | |
$message.= "\n\n"; | |
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n"; | |
$message.= "Content-ID: \<77987_SOME_WEIRD_TOKEN_BUT_UNIQUE_SO_SOMETIMES_A_@domain.com_IS_ADDED\>\n"; | |
$message.= 'Content-Type: application/zip; name="shell.zip"'; | |
$message.= "\n"; | |
$message.= "Content-Transfer-Encoding: base64\n"; | |
$message.= 'Content-Disposition: attachment; filename="shell.zip"'; | |
$message.= "\n"; | |
$message.= base64_encode(file_get_contents("/tmp/gnome_shell___switch_by_half_left-d4yj2qz.zip")); | |
$message.= "\n"; | |
$message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n"; | |
$response = $amazonSes->send_raw_email(array( | |
'Data'=> base64_encode($message)), | |
array('Source'=>$src, 'Destinations'=> $dest)); | |
/*$response = $amazonSes->send_raw_email($mail_array);*/ | |
print_r($response); | |
if (!$response->isOK()) { | |
echo 'Not Sent'; | |
}else { | |
echo 'Sent'; | |
} | |
?> |