Sending Emails With Amazon SES

hunterlong October 6, 2012 0
Sending Emails With Amazon SES

Sending emails from Amazon SES can sometimes be difficult. This script will include the PHP source code to send individual emails, or mass emailing from a MySQL database. We will also be sending the email with an awesome html/css designed email template. You need to include the ses.php file. You can download ses.php from amazon.

SENDING INDIVIDUAL EMAILS 

Sending a basic text email through Amazon SES.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$subject = "This is the Subject";   //put in your subject
$from = "[email protected]";    //put in your from email address, verify on amazon ses.
$addto = $email;   //user email goes here
 
$message = "This is where the message goes. Hello World!";  //basic text email message
 
include_once('ses.php');
$con=new SimpleEmailService("API ID HERE","API KEY HERE");  //insert your api keys here
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m);    //sends the email

$message = "This is where the message goes. Hello World!"; //basic text email message

include_once(‘ses.php’);
$con=new SimpleEmailService("API ID HERE","API KEY HERE"); //insert your api keys here
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m); //sends the email

WITH HTML MESSAGE
This will send email with an HTML template. Looks great on phones! You can download the html and css template here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$subject = "This is the Subject";
$from = "[email protected]";
$addto = $email;   //user email goes here
 
ob_start();
include_once("/emails/htmlmessage.html");    // the location of your html template. Must include all CSS in the HTML. Also, images must have your full domain/location. 
$message = ob_get_contents();
ob_end_clean();
 
$subject = "This is the Subject";
$from = "[email protected]";
$addto = $useremail;   //user email goes here
$con=new SimpleEmailService(“API ID HERE”,”API KEY HERE”);
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m);
echo “Email sent to: $addto”;

ob_start();
include_once("/emails/htmlmessage.html"); // the location of your html template. Must include all CSS in the HTML. Also, images must have your full domain/location.
$message = ob_get_contents();
ob_end_clean();

$subject = "This is the Subject";
$from = "[email protected]";
$addto = $useremail; //user email goes here
$con=new SimpleEmailService(“API ID HERE”,”API KEY HERE”);
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m);
echo “Email sent to: $addto”;

Mass HTML Email Sending
Not much of a difference, this script will send an HTML email to all users in the MySQL database. Create a while loop that goes through all users.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$subject = "This is the Subject";
$from = "[email protected]";
$addto = $email;   //user email goes here
 
ob_start();
include_once("/emails/htmlmessage.html");    // the location of your html template. Must include all CSS in the HTML. Also, images must have your full domain/location. 
$message = ob_get_contents();
ob_end_clean();
 
$qryget = mysql_query("SELECT * FROM users ORDER BY id DESC") or die (mysql_error());
while($row = mysql_fetch_array($qryget)){
 
$useremail= $row['email'];
 
$subject = "This is the Subject";
$from = "[email protected]";
$addto = $useremail;   //user email goes here
$con=new SimpleEmailService(“API ID HERE”,”API KEY HERE”);
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m);
echo “Email sent to: $addto”;
}

ob_start();
include_once("/emails/htmlmessage.html"); // the location of your html template. Must include all CSS in the HTML. Also, images must have your full domain/location.
$message = ob_get_contents();
ob_end_clean();

$qryget = mysql_query("SELECT * FROM users ORDER BY id DESC") or die (mysql_error());
while($row = mysql_fetch_array($qryget)){

$useremail= $row[’email’];

$subject = "This is the Subject";
$from = "[email protected]";
$addto = $useremail; //user email goes here
$con=new SimpleEmailService(“API ID HERE”,”API KEY HERE”);
$con->listVerifiedEmailAddresses();
$m = new SimpleEmailServiceMessage();
$m->addTo($addto);
$m->setFrom($from);
$m->setSubject($subject);
$m->setMessageFromString($message,$message);
$con->sendEmail($m);
echo “Email sent to: $addto”;
}

Have a question? comment and i’ll check out your situation. Please remember to have CURL installed and enabled in PHP settings. Amazon SES html email template

Leave A Response »