Passed
Push — master ( f4ca72...4ad14a )
by luo
01:44
created

Mail::send()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 48
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 48
rs 8.8333
cc 7
nc 6
nop 4
1
<?php
2
/**
3
 * 邮件
4
 *
5
 * @author mybsdc <[email protected]>
6
 * @date 2019/5/12
7
 * @time 16:38
8
 */
9
10
namespace Luolongfei\Lib;
11
12
use PHPMailer\PHPMailer\PHPMailer;
13
use PHPMailer\PHPMailer\Exception AS MailException;
14
15
class Mail
16
{
17
    /**
18
     * @var PHPMailer
19
     */
20
    protected static $mail;
21
22
    /**
23
     * @return PHPMailer
24
     * @throws MailException
25
     * @throws \Exception
26
     */
27
    public static function mail()
28
    {
29
        if (!self::$mail instanceof PHPMailer) {
0 ignored issues
show
introduced by
self::mail is always a sub-type of PHPMailer\PHPMailer\PHPMailer.
Loading history...
30
            self::$mail = new PHPMailer(true);
31
32
            // 邮件服务配置
33
            $username = config('mail.username');
34
            $password = config('mail.password');
35
            if (stripos($username, '@gmail.com') !== false) {
36
                $host = 'smtp.gmail.com';
37
                $secure = 'tls';
38
                $port = 587;
39
            } else if (stripos($username, '@qq.com') !== false) {
40
                $host = 'smtp.qq.com';
41
                $secure = 'tls';
42
                $port = 587;
43
            } else if (stripos($username, '@163.com') !== false) {
44
                $host = 'smtp.163.com';
45
                $secure = 'ssl';
46
                $port = 465;
47
            } else {
48
                throw new \Exception('不受支持的邮箱。目前仅支持谷歌邮箱、QQ邮箱以及163邮箱,推荐使用谷歌邮箱。');
49
            }
50
51
            self::$mail->SMTPDebug = config('debug') ? 2 : 0; // Debug 0:关闭 1:客户端信息 2:客户端和服务端信息
52
            self::$mail->isSMTP(); // 告诉PHPMailer使用SMTP
53
            self::$mail->Host = $host; // SMTP服务器
54
            self::$mail->SMTPAuth = true; // 启用SMTP身份验证
55
            self::$mail->Username = $username; // 账号
56
            self::$mail->Password = $password; // 密码或授权码
57
            self::$mail->SMTPSecure = $secure; // 将加密系统设置为使用 - ssl(不建议使用)或tls
58
            self::$mail->Port = $port; // 设置SMTP端口号 - tsl使用587端口,ssl使用465端口
59
            self::$mail->CharSet = 'UTF-8'; // 防止中文邮件乱码
60
            self::$mail->setLanguage('zh_cn', VENDOR_PATH . '/phpmailer/phpmailer/language/'); // 设置语言
0 ignored issues
show
Bug introduced by
The constant Luolongfei\Lib\VENDOR_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
            self::$mail->setFrom($username, 'im robot'); // 发件人
62
        }
63
64
        return self::$mail;
65
    }
66
67
    /**
68
     * 发送邮件
69
     *
70
     * @param string $subject 标题
71
     * @param string | array $content 正文
72
     * @param string $to 收件人,选传
73
     * @param string $template 模板,选传
74
     *
75
     * @return bool
76
     * @throws \Exception
77
     */
78
    public static function send($subject, $content, $to = '', $template = '')
79
    {
80
        if (config('mail.enable') === false) {
81
            system_log(sprintf('由于没有启用邮件功能,故本次不通过邮件送信。今次邮件标题为:%s', $subject));
82
83
            return false;
84
        }
85
86
        self::mail()->addAddress($to ?: config('mail.to'), config('mail.toName', '主人')); // 添加收件人,参数2选填
0 ignored issues
show
Bug introduced by
It seems like $to ?: config('mail.to') can also be of type array; however, parameter $address of PHPMailer\PHPMailer\PHPMailer::addAddress() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        self::mail()->addAddress(/** @scrutinizer ignore-type */ $to ?: config('mail.to'), config('mail.toName', '主人')); // 添加收件人,参数2选填
Loading history...
Bug introduced by
It seems like config('mail.toName', '主人') can also be of type array; however, parameter $name of PHPMailer\PHPMailer\PHPMailer::addAddress() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        self::mail()->addAddress($to ?: config('mail.to'), /** @scrutinizer ignore-type */ config('mail.toName', '主人')); // 添加收件人,参数2选填
Loading history...
87
        self::mail()->addReplyTo(config('mail.replyTo', '[email protected]'), config('mail.replyToName', '作者')); // 备用回复地址,收到的回复的邮件将被发到此地址
0 ignored issues
show
Bug introduced by
It seems like config('mail.replyToName', '作者') can also be of type array; however, parameter $name of PHPMailer\PHPMailer\PHPMailer::addReplyTo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        self::mail()->addReplyTo(config('mail.replyTo', '[email protected]'), /** @scrutinizer ignore-type */ config('mail.replyToName', '作者')); // 备用回复地址,收到的回复的邮件将被发到此地址
Loading history...
Bug introduced by
It seems like config('mail.replyTo', '[email protected]') can also be of type array; however, parameter $address of PHPMailer\PHPMailer\PHPMailer::addReplyTo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        self::mail()->addReplyTo(/** @scrutinizer ignore-type */ config('mail.replyTo', '[email protected]'), config('mail.replyToName', '作者')); // 备用回复地址,收到的回复的邮件将被发到此地址
Loading history...
88
89
        /**
90
         * 抄送和密送都是添加收件人,抄送方式下,被抄送者知道除被密送者外的所有的收件人,密送方式下,
91
         * 被密送者知道所有的被抄送者,但不知道其它的被密送者。
92
         * 抄送好比@,密送好比私信。
93
         */
94
//        self::mail()->addCC('[email protected]'); // 抄送
95
//        self::mail()->addBCC('[email protected]'); // 密送
96
97
        // 添加附件,参数2选填
98
//        self::mail()->addAttachment('README.md', '说明.txt');
99
100
        // 内容
101
        self::mail()->Subject = $subject; // 标题
102
103
        /**
104
         * 正文
105
         * 使用html文件内容作为正文,其中的图片将被base64编码,另确保html样式为内联形式,且某些样式可能需要!important方能正常显示,
106
         * msgHTML方法的第二个参数指定html内容中图片的路径,在转换时会拼接html中图片的相对路径得到完整的路径,最右侧无需“/”,PHPMailer
107
         * 源码里有加。css中的背景图片不会被转换,这是PHPMailer已知问题,建议外链。
108
         * 此处也可替换为:
109
         * self::mail()->isHTML(true); // 设为html格式
110
         * self::mail()->Body = '正文'; // 支持html
111
         * self::mail()->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'; // 纯文本消息正文。不支持html预览的邮件客户端将显示此预览消息,其它情况将显示正常的body
112
         */
113
        $template = file_get_contents(RESOURCES_PATH . '/mail/' . ($template ?: 'default') . '.html');
0 ignored issues
show
Bug introduced by
The constant Luolongfei\Lib\RESOURCES_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
114
        if (is_array($content)) {
115
            array_unshift($content, $template);
116
            $message = call_user_func_array('sprintf', $content);
117
        } else if (is_string($content)) {
0 ignored issues
show
introduced by
The condition is_string($content) is always true.
Loading history...
118
            $message = sprintf($template, $content);
119
        } else {
120
            throw new MailException('邮件内容格式错误,仅支持传入数组或字符串。');
121
        }
122
123
        self::mail()->msgHTML($message, APP_PATH . '/mail');
0 ignored issues
show
Bug introduced by
The constant Luolongfei\Lib\APP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
124
125
        if (!self::mail()->send()) throw new MailException(self::mail()->ErrorInfo);
126
    }
127
}