Skip to content

Commit b7cd57d

Browse files
committed
添加邮件发送处理函数
1 parent 7ad457a commit b7cd57d

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

application/handler/Email.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
4+
namespace app\handler;
5+
6+
use PHPMailer\PHPMailer\PHPMailer;
7+
use PHPMailer\PHPMailer\Exception;
8+
use think\facade\Log;
9+
10+
/**
11+
* 邮件发送处理
12+
* Class Email
13+
* @package app\handler
14+
*/
15+
class Email
16+
{
17+
public function send($toUserEmail,$name,$title,$content)
18+
{
19+
20+
$mail = new PHPMailer(true);
21+
22+
try {
23+
//Server settings
24+
$mail->SMTPDebug = 0; // Enable verbose debug output
25+
$mail->isSMTP(); // Set mailer to use SMTP
26+
$mail->Host = config('email.host'); // Specify main and backup SMTP servers
27+
$mail->SMTPAuth = true; // Enable SMTP authentication
28+
$mail->Username = config('email.username'); // SMTP username
29+
$mail->Password = config('email.password'); // SMTP password
30+
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
31+
$mail->Port = config('email.port'); // TCP port to connect to
32+
33+
//Recipients
34+
$mail->setFrom(config('email.username'), 'WebBug');
35+
$mail->addAddress($toUserEmail, $name); // Add a recipient
36+
37+
38+
// Attachments
39+
40+
// Content
41+
$mail->isHTML(true); // Set email format to HTML
42+
$mail->Subject = $title;
43+
$mail->Body = $content;
44+
45+
46+
$mail->send();
47+
48+
return true;
49+
} catch (Exception $e) {
50+
51+
Log::info("Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
52+
return false;
53+
}
54+
55+
56+
}
57+
}

application/index/controller/Test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
namespace app\index\controller;
5+
6+
use app\handler\Email;
7+
8+
class Test
9+
{
10+
public function index(Email $email)
11+
{
12+
$email->send('[email protected]','phpzc','test','<h1>test</h1>');
13+
}
14+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": ">=5.6.0",
2020
"topthink/framework": "5.1.*",
21-
"topthink/think-captcha": "^2.0"
21+
"topthink/think-captcha": "^2.0",
22+
"phpmailer/phpmailer": "^6.0"
2223
},
2324
"autoload": {
2425
"psr-4": {

config/email.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'host' => 'smtp.qq.com',
5+
'port' => '465',
6+
'is_ssl' => true,
7+
'username' => '[email protected]',
8+
'password' => 'gjvtjszssgvtiefh',
9+
];

0 commit comments

Comments
 (0)