| Conditions | 9 |
| Paths | 7 |
| Total Lines | 55 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 87 | public static function send($subject, $content, $to = '', $template = '') |
||
| 88 | { |
||
| 89 | if (config('mail.enable') === false) { |
||
| 90 | system_log('由于没有启用邮件功能,故本次不通过邮件送信。'); |
||
| 91 | |||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | $to = $to ?: config('mail.to'); |
||
| 96 | if (!$to) { |
||
| 97 | throw new LlfException(env('ON_GITHUB_ACTIONS') ? 34520011 : 34520012); |
||
| 98 | } |
||
| 99 | |||
| 100 | self::mail()->addAddress($to, config('mail.toName', '主人')); // 添加收件人,参数2选填 |
||
| 101 | self::mail()->addReplyTo(config('mail.replyTo', '[email protected]'), config('mail.replyToName', '作者')); // 备用回复地址,收到的回复的邮件将被发到此地址 |
||
| 102 | |||
| 103 | /** |
||
| 104 | * 抄送和密送都是添加收件人,抄送方式下,被抄送者知道除被密送者外的所有的收件人,密送方式下, |
||
| 105 | * 被密送者知道所有的被抄送者,但不知道其它的被密送者。 |
||
| 106 | * 抄送好比@,密送好比私信。 |
||
| 107 | */ |
||
| 108 | // self::mail()->addCC('[email protected]'); // 抄送 |
||
| 109 | // self::mail()->addBCC('[email protected]'); // 密送 |
||
| 110 | |||
| 111 | // 添加附件,参数2选填 |
||
| 112 | // self::mail()->addAttachment('README.md', '说明.txt'); |
||
| 113 | |||
| 114 | // 内容 |
||
| 115 | self::mail()->Subject = $subject; // 标题 |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 正文 |
||
| 119 | * 使用html文件内容作为正文,其中的图片将被base64编码,另确保html样式为内联形式,且某些样式可能需要!important方能正常显示, |
||
| 120 | * msgHTML方法的第二个参数指定html内容中图片的路径,在转换时会拼接html中图片的相对路径得到完整的路径,最右侧无需“/”,PHPMailer |
||
| 121 | * 源码里有加。css中的背景图片不会被转换,这是PHPMailer已知问题,建议外链。 |
||
| 122 | * 此处也可替换为: |
||
| 123 | * self::mail()->isHTML(true); // 设为html格式 |
||
| 124 | * self::mail()->Body = '正文'; // 支持html |
||
| 125 | * self::mail()->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'; // 纯文本消息正文。不支持html预览的邮件客户端将显示此预览消息,其它情况将显示正常的body |
||
| 126 | */ |
||
| 127 | $template = file_get_contents(RESOURCES_PATH . '/mail/' . ($template ?: 'default') . '.html'); |
||
| 128 | if (is_array($content)) { |
||
| 129 | array_unshift($content, $template); |
||
| 130 | $message = call_user_func_array('sprintf', $content); |
||
| 131 | } else if (is_string($content)) { |
||
| 132 | $message = $content; |
||
| 133 | } else { |
||
| 134 | throw new MailException('邮件内容格式错误,仅支持传入数组或字符串。'); |
||
| 135 | } |
||
| 136 | |||
| 137 | self::mail()->msgHTML($message, APP_PATH . '/mail'); |
||
| 138 | |||
| 139 | if (!self::mail()->send()) throw new MailException(self::mail()->ErrorInfo); |
||
| 140 | |||
| 141 | return true; |
||
| 142 | } |
||
| 144 |