اتصال به ایمیل‌سرور در برنامه‌های Laravel


پروژه و کدهای مورد استفاده در ویدیوی فوق در اینجا قابل مشاهده و دسترسی هستند.

برای استفاده از سرویس ایمیل در برنامه‌های Laravel، کافیست تا طبق مستندات SMTP، یک دسترسی SMTP و طبق مستندات افزودن نشانی، یک نشانی برای ایمیل‌سرور خود، ایجاد کنید. در نهایت نیز، بایستی اطلاعات مربوط به ایمیل‌سرور خود را به متغیرهای محیطی برنامه خود، اضافه کنید؛ به عنوان مثال:

کپی
MAIL_DRIVER=smtp
MAIL_HOST=smtp.c1.liara.email
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=my-app
MAIL_PASSWORD=87b9307a-dae9-410e-89a2-e77de60e4885

با تنظیم MAIL_ENCRYPTION=tls، می‌توانید به‌صورت امن اقدام به ارسال ایمیل‌های تراکنشی کنید.

برای اتصال برنامه به ایمیل‌سرور، باید با اجرای دستور زیر، یک کلاس Mailable ایجاد کنید:

کپی
php artisan make:mail NotifyMail

سپس به دایرکتوری app/mail بروید و فایل notifyMail.php را باز کنید و کد زیر را به آن اضافه کنید:

کپی
<?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateMailMailablesContent;
use IlluminateMailMailablesEnvelope;
use IlluminateQueueSerializesModels;

class NotifyMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Notify Mail',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.demoMail',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, IlluminateMailMailablesAttachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

سپس، یک قالب ایمیل با نام demoMail.blade.php در مسیر resources/views/emails ایجاد کنید و قطعه کد زیر را، در آن، قرار دهید:

کپی
<!DOCTYPE html>
<html>
<head>
  <title>Laravel email example with Liara</title>
</head>
<body>

  <h1>This is a test mail to see how Liara works.</h1>
  <p>Laravel 10 send email example.</p>

</body>
</html> 

سپس، در routes/web.php مسیرهای زیر را برای ارسال ایمیل اضافه کنید:

کپی
use App\Http\Controllers\SendEmailController;

Route::post('send-email', [SendEmailController::class, 'index']);

در ادامه، با استفاده از دستور زیر یک کنترلر جدید ایجاد کنید:

کپی
php artisan make:controller SendEmailController

در نهایت قطعه کد زیر را در app/Http/Controllers/SendEmailController.php، وارد کنید:

کپی
<?php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
use Mail;
  
use App\Mail\NotifyMail;
  
  
class SendEmailController extends Controller
{
      
  public function index()
  {
    Mail::to('destination@gmail.com')->send(new NotifyMail());
  
    return "Great! Your email has been sent successfully.";
  } 
}

با انجام کارهای فوق، می‌توانید از ایمیل‌سرور در برنامه خود، استفاده کنید.