Laravel的邮件发送与通知:发送电子邮件、短信和其他形式的通知

网络安全侦探 2019-02-26 ⋅ 23 阅读

在现代的Web应用中,发送电子邮件、短信和其他形式的通知是非常常见的需求。Laravel 框架提供了一种简洁而强大的方式来管理这些通知,无论是发送电子邮件,还是发送短信,甚至是通过其他驱动程序发送通知。

配置邮件发送

在 Laravel 中配置邮件发送非常简单,并且支持多种邮件驱动程序,包括 SMTP、Mailgun、Mandrill、Amazon SES 等等。在 .env 文件中,你可以设置用于邮件发送的驱动程序、SMTP 配置、API 密钥等。

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

更改驱动程序后,你可能还需要调整其他配置选项,比如 SMTP 设置或者 API 密钥。这些配置选项都可以在 .env 文件中进行设置。

创建邮件

在 Laravel 中,你可以使用 php artisan make:mail 命令来生成一个新的邮件类。该命令将在 app/Mail 目录中创建一个新的邮件类,类名会具有 Mailable 后缀。

php artisan make:mail WelcomeMail

生成的邮件类将包含一个 build 方法,该方法用于定义邮件的内容、主题和附件。下面是一个简单示例:

namespace App\Mail;

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

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome')
                    ->subject('Welcome to My Website')
                    ->attach(public_path('/path/to/attachment.pdf'));
    }
}

build 方法中,我们可以使用 view 方法指定邮件的视图模板,使用 subject 方法设置主题,使用 attach 方法添加附件。

发送邮件

一旦创建了邮件类,你可以使用 Mail facade 来发送邮件。下面是一个发送邮件的示例:

use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;

public function sendWelcomeEmail($email)
{
    Mail::to($email)->send(new WelcomeMail());
}

在上面的示例中,我们使用 Mail facade 的 to 方法指定接收邮件的地址,并使用 send 方法发送邮件。

配置短信发送

Laravel 还提供了一种简单的方式来配置和发送短信通知。在 .env 文件中,你可以设置用于短信发送的驱动程序、用户名、密码等。

SMS_DRIVER=log
SMS_FROM=YourPhoneNumber
SMS_API_KEY=your-api-key
SMS_API_SECRET=your-api-secret

类似于邮件发送,你可以在 .env 文件中配置你所选用的短信驱动程序和其他相关设置。

创建短信通知

在 Laravel 中,你可以使用 php artisan make:notification 命令来生成一个新的短信通知类。该命令将在 app/Notifications 目录中创建一个新的通知类。

php artisan make:notification WelcomeNotification

生成的短信通知类将包含一个 toSms 方法(可以自定义)和 toArray 方法(用于将通知转化为数组)。下面是一个示例:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Notification;

class WelcomeNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['nexmo'];
    }

    /**
     * Get the Nexmo / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toSms($notifiable)
    {
        return (new NexmoMessage())
                    ->content('Welcome to My Website');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

toSms 方法中,我们可以使用 NexmoMessage 类来设置短信通知的内容。

发送短信

一旦创建了短信通知类,你可以通过调用 notify 方法来发送短信通知。下面是一个发送短信通知的示例:

use App\Notifications\WelcomeNotification;
use Illuminate\Support\Facades\Notification;

public function sendWelcomeSms($phoneNumber)
{
    Notification::route('nexmo', $phoneNumber)
                ->notify(new WelcomeNotification());
}

在上面的示例中,我们使用 Notification facade 的 route 方法指定接收短信通知的手机号码,并使用 notify 方法发送短信通知。

其他通知方式

除了电子邮件和短信,Laravel 还支持其他通知方式,如 Slack、数据库、广播等等。你可以在通知类的 via 方法中添加所需的通知方式。

public function via($notifiable)
{
    return ['mail', 'database', 'slack'];
}

然后,在通知类的相应方法中,你可以根据需要定制通知的内容。

总结

Laravel 提供了强大而灵活的邮件发送和通知系统,让你能够轻松地发送电子邮件、短信和其他形式的通知。通过配置邮件发送驱动程序和相关设置,在邮件类中定义邮件的内容、主题和附件,并使用 Mail facade 发送电子邮件。类似地,通过配置短信发送驱动程序和相关设置,在短信通知类中定义短信的内容,使用 Notification facade 发送短信通知。此外,你还可以通过添加其他通知方式,如 Slack、数据库、广播等来丰富你的通知系统。


全部评论: 0

    我有话说: