Laravel Send mail based on SMTP driver implementation

Source: Internet
Author: User
Tags email account

PHP 7
Laravel 5.1
OS X El Capitan 10.11.4
Brief introduction
Laravel provides a concise API based on the popular Swiftmailer function library. Laravel provides drivers for SMTP, Mailgun, Mandrill, Amazon SES, PHP mail functions, and sendmail, allowing you to quickly start sending messages from your chosen local or cloud service. (excerpt Phphub translation document)

Configuration

The mail profile is config/mail.php

return [
Default Send Message Driver
' Driver ' => env (' Mail_driver ', ' SMTP '),

Send mail host Address
' Host ' => env (' mail_host ', ' smtp.mailgun.org '),

Send mail host port
' Port ' => env (' Mail_port ', 587),

Specify the mailbox address and user name for sending messages
' From ' => [' address ' => null, ' name ' => null],

Specify the Send mail protocol
' Encryption ' => env (' mail_encryption ', ' TLS '),

e-Mail login account
' Username ' => env (' Mail_username '),

Mailbox Login Password
' Password ' => env (' Mail_password '),

Specifies the driven command address when the driver is SendMail
' SendMail ' => '/usr/sbin/sendmail-bs ',

False send message does not log, true log does not send mail
' Pretend ' => false,
];

Writing Programs

Env
This article uses the QQ mailbox to carry on the test, first modifies the mailbox configuration

Mail_driver=smtp
Mail_host=smtp.qq.com
mail_port=465
Mail_username= (fill in QQ email account)
Mail_password= (fill in QQ email password)
Mail_encryption=ssl
Routing
/* Mail Management module * *
Route::get (' Email/send/{id} ', [
' As ' => ' backend.email.send ',
' Uses ' => ' emailcontroller@send ',
]);

Controller

New Controller

PHP Artisan Make:controller Backend/emailcontroller--plain
The controller code is as follows

<?php

namespace App\http\controllers\backend;

Use App\facades\userrepository;
Use Illuminate\http\request;

Use app\http\requests;
Use App\http\controllers\controller;

Use Mail;

Class Emailcontroller extends Controller
{
Public function Send (Request $request, $id)
{
$user = Userrepository::find ($id);

$result = Mail::send (' emails.test ', [' User ' => $user], function ($email) use ($user) {
$email->to (' 2794408425@qq.com ')->subject (' Hello world ');
});

if ($result) {
Echo ' send mail successfully ';
} else {
Echo ' failed to send message ';
}
}
}

New View

New View emails/test.blade.php, the code is as follows:

<title></title>
<body>
Hello, {{$user->name}}, this is a test message.
</body>

Executing code

Access the specified route in the browser, and then go to the mailbox to see if the message was sent successfully.


To see if a message was sent successfully

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.