Send email via AWS SES in Laravel

  1. Apply for AWS SES (simple email service)
  2. Create an user, and SES full accessible policy, along with the access key and secret key.
  3. Go to AWS console, click email address option on left side, and verify your email.

  1. Go to AWS support center to submit a service limit increase case, and choose SES Sending Limits, called ‘Remove it out of SandBox’ Otherwise the capacity of the mail you are allowed to send and its rate will be limited to a large extend, besides that, all of the recipients will have to be verified by SES.

  1. Create a Laravel Project

  2. Install mail package composer require guzzlehttp/guzzle

  3. Install AWS SDK composer require aws/aws-sdk-php

  4. Go to config/mail.php, and revise the driver toses

  5. Go to config/services.php, and config it as follows:

    'ses' => [
    'key' => 'your-ses-key',
    'secret' => 'your-ses-secret',
    'region' => 'ses-region', // e.g. us-east-1
    ],
  6. You should config all above mentioned in your .env file as follows:

    MAIL_DRIVER=ses
    MAIL_FROM_ADDRESS=your-mail-address
    MAIL_FROM_NAME=BuyBuyGo
    SES_KEY=your-ses-key
    SES_SECRET=your-ses-secret
    SES_REGION=us-west-2
  7. Create class,php artisan make:mail OrderCreated --markdown=emails.orders.created

  8. Go to OrderCreated.php, config your build as follows:

    <?php

    namespace App\Mail;

    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;

    class OrderShipped extends Mailable
    {
    use Queueable, SerializesModels;

    protected $order;

    /**
    * Create a new message instance.
    *
    * @return void
    */
    public function __construct($order)
    {
    $this->order = $order;
    //
    }

    /**
    * Build the message.
    *
    * @return $this
    */
    public function build()
    {
    return $this->markdown('emails.orders.created')
    ->with([
    'buyer' => $this->order->user->name,
    'order' => $this->order->name,
    'item_name' => $this->order->item_name,
    'item_description' => $this->order->item_description,
    'quantity' => $this->order->quantity,
    'total_amount' => $this->order->total_amount,
    'unit_price' => $this->order->unit_price,
    'expiry_time' => $this->order->expiry_time,
    ]);
    }
    }
  9. Go to created.blade.php to customize the view as follows:

    @component('mail::message')
    # Dear {{ $buyer }}
    Thanks for your patronage!

    - Order: {{$order}}
    - Item: {{$item_name}}
    - Item description: {{$item_description}}
    - Quantity: {{$quantity}}
    - Unit price: {{$unit_price}}
    - Amount: {{$total_amount}}

    ## Kindly make this payment before <span style="color: red">{{$expiry_time}}</span>

    <hr>

    <br>
    ## If you have any question, feel free to contact us

    @component('mail::button', ['url' => 'https://tn710617.github.io/'])
    Contact Us
    @endcomponent

    Thanks,<br>
    {{ config('app.name') }}
    @endcomponent
  10. Now you could use mail to send email wherever you want.

    Mail::to($buyer->email)->send(new OrderCreated($order));
  11. Now it should work

  12. Do you think “that’s it?”, yeah almost. However, there is one more thing.
    I spent one day figuring out all above mentioned, and then I came across something tricky, and it took me one another day.
    As a backend programmer, whenever I need to connect a third party payment service, I use ngrok to help me develop.
    It’s so weird this time. I made my script to do some things in my controller after I received the response from payment service. However, it did every function I wrote except for the mail function… and here is the error message:

    "message": "Expected response code 250 but got code \"530\", with message \"530 5.7.1 Authentication required\r\n\"",
    "exception": "Swift_TransportException",
    "file": "/Users/ray/code/FacebookOptimizedSellingSystem/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php",
    "line": 457,
    "trace": [

    1000 lines omitted.

After tormenting debugging, I found out if I used valet share, this error would not occur, and if I used php artisan serve --port=yourPort, and then ngrok http yourPort, and the error came out.

although I solved the error eventually, to be honest I still didn’t know why..

If you know why the error occurred, kindly drop me a message or a mail, it will really help!

Task Scheduling of Laravel How to configure Git?

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×