laravel 傳送郵件以及引數配置

shadowbook發表於2019-02-16

配置引數檔案路徑: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 AppLibrarys;
use IlluminateSupportFacadesMail;

/**
 * 傳送郵件類
 * Class SendMail
 * @package AppLibrarys
 */
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>

相關文章