laravel 发送邮件以及参数配置

配置参数文件路径:app/config/mail.php

return [
     'driver' => env('MAIL_DRIVER', 'smtp'),
     'host' => env('MAIL_HOST', 'smtp.qiye.163.com'),
     'port' => env('MAIL_PORT', 465),
     'from' => ['address' => 'myemail@163.com', 'name' => 'My Email'],
     'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
     'username' => env('MAIL_USERNAME', 'myemail@163.com'),
     'password' => env('MAIL_PASSWORD', 'xxxx'),
];

php代码

<?php

namespace App\Librarys;
use Illuminate\Support\Facades\Mail;

/**
 * 发送邮件类
 * Class SendMail
 * @package App\Librarys
 */
class SendMail
{

    /* 调用方法
$user = ['andy.chen@qq.com'];
$view = 'test.mail';
$args = ['name' => 'Andy'];
$test = '邮件内容';
$mailTitle = '邮件标题';
try{
    $res = SendMail::sendViewEmail($user, $mailTitle, $view, $args);
    //$res = SendMail::sendTextEmail($user, $mailTitle, $test);
    dd($res);
} catch (\Exception $e) {
    dd($e);
}
     */

    /**
     * 发送视图邮件
     * @date 2017-03-08 15:17:19
     *
     * @param 用户邮箱|array|string $user
     * @param 邮件标题|string $mailTitle
     * @param 视图|string $view
     * @param 视图变量|array $args
     *
     * @return bool
     */
    public static function sendViewEmail($user, $mailTitle, $view, $args)
    {
        if ( ! is_array($user)) {
            $user = [$user];
        }
        return Mail::send($view, ['args' => $args], function ($m) use ($user, $mailTitle) {
            $m->to($user)->subject($mailTitle);
        });
    }

    /**
     * 发送文本邮件
     * @date 2017-03-08 15:38:40
     *
     * @param 用户邮箱|array|string $user
     * @param 邮件标题|string $mailTitle
     * @param 文本内容|string $text
     *
     * @return mixed
     */
    public static function sendTextEmail($user, $mailTitle, $text)
    {
        if ( ! is_array($user)) {
            $user = [$user];
        }
        return Mail::raw($text, function ($m) use ($user, $mailTitle) {
            $m ->to($user)->subject($mailTitle);
        });
    }

}

view代码

<!DOCTYPE html>
<html>
    <head>
        <title>Mail Test Reminder</title>


        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                color: #B0BEC5;
                display: table;
                font-weight: 100;
                font-family: 'Lato';
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }

            .title {
                font-size: 72px;
                margin-bottom: 40px;
            }
            .text {
                font-size: 22px;
                margin-bottom: 40px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">{{$args['name']}}</div>
                <div class="text">Mail Test Reminder</div>
            </div>
        </div>
    </body>
</html>
    原文作者:shadowbook
    原文地址: https://segmentfault.com/a/1190000008626243
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞