Amy Neville
Amy Neville

Reputation: 10581

Amazon Simple Email Service - Need to send email

I'm trying to send an email using SES and the PHP SDK v3. But it's proving rather difficult. This is as far as I've got:

require_once '/src/aws/aws-autoloader.php';

$ses = new Aws\Ses\SesClient([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => [
        'key'    => 'mykeyhere',
        'secret' => 'mysecrethere',
    ],
]);

I've tried copying code from various guides, but none of it seems to work. Can someone show me how to send an email with php?

Upvotes: 0

Views: 2236

Answers (2)

pedrofernandes
pedrofernandes

Reputation: 16854

I know you asked how to use in SES API, but I use PHPMailer for this task. result will be same.

<?php
    require (dirname(__FILE__) . '/phpmailer/PHPMailerAutoload.php');

    if ($_SERVER['REQUEST_METHOD'] == "POST")  {
        $name    = $_POST['name'];      
        $email   = $_POST['email'];
        $msg     = $_POST['message'];

        // send email
        $mail = new PHPMailer();
        $mail->isSMTP();     
        $mail->isHTML(false);       
        $mail->Host = 'email-smtp.us-west-2.amazonaws.com';  // Or put your zone here
        $mail->Port = 587;
        $mail->SMTPAuth = true;        
        $mail->SMTPSecure = "tls";          
        $mail->Username = 'mykeyhere';                          
        $mail->Password = 'mysecrethere';  

        $mail->setFrom('<email added in ses>', "$name");
        $mail->addAddress('<destination email>');    

        $mail->Subject = "design2co.de";
        $mail->Body    = "Nome: $name \n" .
                         "Email: $email \n" .
                         "Mensagem: $msg \n";

        if(!$mail->send()) {
            echo json_encode(array("status" => 404));
        } else {
            echo json_encode(array("status" => 200));
            exit;
    }
} 
?>

Upvotes: 0

Justinas
Justinas

Reputation: 43441

Using AWS API V2

First set up your SES:

$ses = Aws\Ses\SesClient::factory([
    'key' => 'AWS_KEY',
    'secret' => 'AWS_SECRET_KEY',
    'region' => 'us-east-1'
]);

Than send email:

$ses->sendEmail([
    'Source' => '[email protected]',
    'Destination' => [
        'ToAddresses' => array('[email protected]')
    ],
    'Message' => array(
        'Subject' => [
            'Data' => 'SES Testing',
            'Charset' => 'UTF-8',
        ],
        'Body' => [
            'Html' => [
                'Data' => '<b>My HTML Email</b>',
                'Charset' => 'UTF-8',
            ],
        ],
    ),
]);

Few things to note

  1. You have to verify your sender email or sender url to use SES.
  2. If you don't Request Production Access, than you have to verify all receivers emails.
  3. You can use any PHP library to send using SES, no need for AWS Api

Upvotes: 3

Related Questions