لاراول از کتابخانه SwiftMailer برای ارسال ایمیلهاش استفاده میکنه.
برای ارسال ایمیل ابتدا تنظیمات لازم را در فایل config/mail.php انجام میدهیم:
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
| "sparkpost", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'sendmail'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
که اکثر تنظیماتش از .env میخونه.
حالا در ترمینال کد زیر را میزنیم:
php artisan make:mail DemoEmail
یه فایل داخل app/Mail به اسم DemoEmail ساخته میشه.
فایل رو باز میکنیم و اطلاعات زیر رو توش میزاریم:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class DemoEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* The demo object instance.
*
* @var Demo
*/
public $demo;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($demo)
{
$this->demo = $demo;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('sender@example.com')
->view('mails.demo')
->text('mails.demo_plain')
->with(
[
'testVarOne' => '1',
'testVarTwo' => '2',
])
->attach(public_path('/images').'/demo.jpg', [
'as' => 'demo.jpg',
'mime' => 'image/jpeg',
]);
}
حالا فایل resources/views/mails/demo.blade.php را میسازیم و باز میکنیم و اطلاعات زیر رو توش میزاریم:
Hello <i>{{ $demo->receiver }}</i>,
<p>This is a demo email for testing purposes! Also, it's the HTML version.</p>
<p><u>Demo object values:</u></p>
<div>
<p><b>Demo One:</b> {{ $demo->demo_one }}</p>
<p><b>Demo Two:</b> {{ $demo->demo_two }}</p>
</div>
<p><u>Values passed by With method:</u></p>
<div>
<p><b>testVarOne:</b> {{ $testVarOne }}</p>
<p><b>testVarTwo:</b> {{ $testVarTwo }}</p>
</div>
Thank You,
<br/>
<i>{{ $demo->sender }}</i>
و بعد فایل resources/views/mails/demo_plain.blade.php رو میسازیم و باز میکنیم:
Hello {{ $demo->receiver }},
This is a demo email for testing purposes! Also, it's the HTML version.
Demo object values:
Demo One: {{ $demo->demo_one }}
Demo Two: {{ $demo->demo_two }}
Values passed by With method:
testVarOne: {{ $testVarOne }}
testVarOne: {{ $testVarOne }}
Thank You,
{{ $demo->sender }}
حالا فرض میکنیم یه کنترل داریم به اسم MailController که از طریق اون میخوایم ارسال رو انجام بدیم:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\DemoEmail;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function send()
{
$objDemo = new \stdClass();
$objDemo->demo_one = 'Demo One Value';
$objDemo->demo_two = 'Demo Two Value';
$objDemo->sender = 'SenderUserName';
$objDemo->receiver = 'ReceiverUserName';
Mail::to("receiver@example.com")->send(new DemoEmail($objDemo));
}
}
به همین سادگی 🙂
دیدگاهتان را بنویسید