Skip to content

Instantly share code, notes, and snippets.

@thatryan
Created August 8, 2017 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thatryan/6076cb3165c559f66793745c5b9c9fa5 to your computer and use it in GitHub Desktop.
Save thatryan/6076cb3165c559f66793745c5b9c9fa5 to your computer and use it in GitHub Desktop.
SES mail send
<?php
// Replace us-west-2 with the AWS Region you're using for Amazon SES.
define('REGION','us-east-1');
// require REQUIRED_FILE;
require '/home/ubuntu/vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Ses\Exception\SesException;
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => REGION
));
$dest_email_id = 'verified@mail.com';
$source_email_id = 'anotherverified@mail.com';
$replyto_email_id = 'anotherverified@mail.com';
$subject = 'my subject';
$csv_file = dirname(__FILE__) .'/uploads/filename.csv';
$file_size = filesize($csv_file);
$handle = fopen($csv_file, "r");
$content = fread($handle, $file_size);
$content = chunk_split(base64_encode($content));
$header = "";
$message = "some body text.";
$uid = md5(uniqid(time()));
$header = "From: ".$source_email_id." <".$source_email_id.">\r\n";
$header .= "Reply-To: ".$replyto_email_id."\r\n";
$header .= "To: ".$dest_email_id."\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$csv_file."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$csv_file."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$msg['RawMessage']['Data'] = base64_encode($header);
$msg['RawMessage']['Source']= $source_email_id;
$msg['RawMessage']['Destinations'] = array($dest_email_id);
fclose($handle);
try{
$result = $client->sendRawEmail($msg);
error_log("Email sent! Message ID: ".$result->get('MessageId')."\n");
} catch (SesException $e) {
error_log("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment