Until February 22, 2016, I used Mandrill as the email service in most of my projects that send email. On that date, Mandrill announced a change to its terms: within sixty days it would suspend the accounts of users without a paid Mailchimp account.
As an alternative, in some of my projects I used Sendgrid, a perk of the GitHub Student Developer Pack. Here is what Sendgrid offers: the student plan gives you 15,000 free emails per month if you are a student (normally it is 200 free emails per day).
We will send email from Laravel through Sendgrid over SMTP on port 587, and it will be remarkably easy and enjoyable.
If you look at the app/config/mail.php file of your Laravel 5 project, you will see a mail configuration similar to the following (I removed the comment lines to save space):
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => env('MAIL_FROM'), 'name' => env('MAIL_NAME')],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Sendgrid requires our username and password to send email, and I have always considered it wrong to write passwords directly into files, at least in plain text. So we will keep our password out of sight with some encoding, however simple.
Base64 is an extremely simple scheme that can be encoded and decoded. We will encode our password here and hand Sendgrid the version that PHP decodes. First, go to the Base64 Decode and Encode - Online site, type our Sendgrid password into the first box and click the > ENCODE < button. Ç0kKarma$Lkßir$ifre as input, it will produce w4cwa0thcm1hJExrw59pciRpZnJl. Then update your mail configuration file as follows.
<?php
return [
'driver' => 'smtp',
'host' => 'smtp.sendgrid.net',
'port' => 587,
'from' => array('address' => 'gondericiEposta@Adresi.Com', 'name' => 'Gönderici İsmi'),
'encryption' => 'tls',
'username' => 'SendGrid Kullanıcı Adınız',
'password' => base64_decode('w4cwa0thcm1hJExrw59pciRpZnJl'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
By default, Laravel sets these configuration keys up as .env, assuming they will come from the env(‘MAIL_DRIVER’, ‘smtp’) file. This means: if the MAIL_DRIVER variable is defined in the .env file, use it; otherwise default to smtp. Here we have overridden that kind of definition, but you can configure it however suits you.
We have configured all the parameters Sendgrid needs, so our application is now ready to send email. Let's write a simple route to send one:
Route::get('/eposta', function()
{
$data = ['ad' => 'Tuana Şeyma', 'soyad' => 'Eldem'];
Mail::send('emails.gunaydin', $data, function($message) use($data)
{
$message->to('tuanaseyma@sinaneldem.com.tr', $data['ad'])
->subject('Günaydın Kızım!')
->replyTo('sinan@sinaneldem.com.tr', 'Sinan Eldem');
});
});
The first parameter we pass to the Mail class's send method is the view file to be used; we will set it up shortly. The second parameter is the $data array, used to pass data to the email view. The third parameter is the anonymous closure receiving the message, where the sending happens. The fourth part, use, is optional; I used it below to pass the Recipient's Name parameter.
$message->to('tuanaseyma@sinaneldem.com.tr', $data['ad'])
This is the line where we define the recipient; the first parameter is the Recipient Email Address and the second, optional, parameter is the Recipient Name.
->subject('Günaydın Kızım!');
This line sets the subject of our email.
Finally, we also define the replyTo option here, used when the reply address differs from the sender. It is optional.
->replyTo('sinan@sinaneldem.com.tr', 'Sinan Eldem');
Here, the replyTo passed to the Sender Name is optional.
So far so good; now let's create the view file for our email.
resources/views/emails/ directory, create a gunaydin.blade.php file with the following content:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>E-posta Denemesi</title>
</head>
<body>
Günaydın Sevgili {{ $ad }} {{ $soyad }},
<br>
Gününün aydın olmasını dilerim.
<br>
Sevgiler,
<br>
-Baban
</body>
</html>
As you can see, the variables we sent with the $data array are passed straight into the view.
Our project is now ready to send email. Point your browser to http://projenizinadr.esi/eposta and wait. Depending on your connection speed, this may take a few seconds.
Now open your email client and check your message:
