Skip to content

Instantly share code, notes, and snippets.

@kilitbilgi
Last active April 18, 2017 06:51
Show Gist options
  • Save kilitbilgi/f697898339e3e751c32c735ac9c4ba1d to your computer and use it in GitHub Desktop.
Save kilitbilgi/f697898339e3e751c32c735ac9c4ba1d to your computer and use it in GitHub Desktop.
Send emails with Amazon SNS in PHP
//Include PHP Mailer -> https://github.com/PHPMailer/PHPMailer
require_once(APPPATH.'libraries/phpmailer/PHPMailerAutoload.php');
//Send email with Amazon SNS
function sendMail($data){
$from_email = $data["from_email"];
$from_name = $data["from_name"];
$to_email = $data["to_email"];
$to_name = $data["to_name"];
$subject = $data["subject"];
$body = $data ["body"];
$attachmentIncluded = $data["attachmentIncluded"];
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//You can set your region according to your account. Ex:eu-east-1
$mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = AWS_SES_API_KEY;
//Password to use for SMTP authentication
$mail->Password = AWS_SES_SECRET_KEY;
//Set who the message is to be sent from
$mail->setFrom($from_email, $from_name);
//Set who the message is to be sent to
$mail->addAddress($to_email, $to_name);
//Set the subject line
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
if($attachmentIncluded) {
$attachment = $data["attachment"];
$mail->addAttachment($attachment);
}
//send the message, check for errors
if (!$mail->send()) {
return $mail->ErrorInfo;
}else {
return true;
}
}
@kilitbilgi
Copy link
Author

kilitbilgi commented Apr 18, 2017

This is example request of how its done

$data = array(
"from_email" => "from@email.com",
"to_email" => "to@email.com",
"to_name" => "to_name",
"subject" => "This is subject",
"body" => "Hello sir, this is your awesome body, you can also use html tag.",
"attachmentIncluded" => true,
"attachment"=> "/path/to/file"
);
sendMail($data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment