PHPMailer 发送邮件

下载PHPMailer,下载完成会出现vendor文件夹

vendor文件夹内有下载好的phpmailer

 

 

 phpmailer需要打开php.ini的 sockets 扩展和 openssl 的扩展。

 

创建sendemail.html和sendemail.php放在和vendor同一目录下

 

 

 

文件sendemail.php,需要补充发件人邮箱和授权码

 1 <?php
 2 // Import PHPMailer classes into the global namespace
 3 // These must be at the top of your script, not inside a function
 4 use PHPMailer\PHPMailer\PHPMailer;
 5 use PHPMailer\PHPMailer\SMTP;
 6 use PHPMailer\PHPMailer\Exception;
 7 
 8 header("Content-Type: text/html; charset=utf-8");
 9 sendemail($_POST['addressee'], $_POST['address'], $_POST['title'], $_POST['content']);
10 
11 function sendemail($addressee, $address, $title, $content)
12 {
13    // Load Composer's autoloader
14    require 'vendor/autoload.php';
15 
16    // Instantiation and passing `true` enables exceptions
17    $mail = new PHPMailer(true);
18 
19    try {
20       //服务器配置
21       $mail->CharSet = "UTF-8";                //设定邮件编码 
22       $mail->SMTPDebug = 0;                   // 调试模式输出
23       $mail->isSMTP();                        // Send using SMTP
24       $mail->Host       = 'smtp.163.com';     // Set the SMTP server to send through
25       $mail->SMTPAuth   = true;                 // 允许 SMTP 认证
26       $mail->Username   = '';   // SMTP 用户名  即邮箱的用户名
27       $mail->Password   = '';             // SMTP 密码  部分邮箱是授权码,例如163邮箱
28       $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // 允许 TLS 或者ssl协议
29       $mail->Port       = 25;                  // 服务器端口 25 或者465 具体要看邮箱服务器支持
30 
31       //Recipients
32       $mail->setFrom('', 'user'); //发件人
33       $mail->addAddress($address, $addressee);  // 收件人
34       // $mail->addAddress('ellen@example.com');// 可添加多个收件人
35       // $mail->addReplyTo('', 'Information'); //回复的时候回复给哪个邮箱 建议和发件人一致
36       // $mail->addCC('cc@example.com');//抄送 
37       // $mail->addBCC('bcc@example.com');//密送
38 
39       // Attachments 发送附件
40       // $mail->addAttachment('/var/tmp/file.tar.gz');// Add attachments
41       // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // 发送附件并且重命名
42 
43       // Content
44       //是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容 
45       $mail->isHTML(true);  // Set email format to HTML
46       $mail->Subject = $title;
47       $mail->Body    = $content . '<hr>';
48       $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
49 
50       $mail->send();
51       echo '邮件发送成功,请去邮箱确认';
52    } catch (Exception $e) {
53       echo "邮件发送失败,Mailer Error: {$mail->ErrorInfo}";
54    }
55 }

文件sendemail.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <title>Document</title>
 9 </head>
10 
11 <body>
12     <h1>发送邮件</h1>
13     <form method="POST" action="sendemail.php" style="border: 1px solid black;padding: 20px;width:360px;">
14         <div>
15             <input type="text" name="addressee" placeholder="收件人">
16         </div>
17         <div>
18             <input type='email' name="address" placeholder="电子邮件地址">
19         </div>
20         <div>
21             <input type="text" name="title" placeholder="标题">
22         </div>
23         <textarea rows="5" cols="47" name="content">输入你的邮件内容</textarea>
24         <div style="text-align: right;">
25             <input type="submit" value="发送邮件" id="send">
26         </div>
27     </form>
28 </body>
29 
30 </html>

 

posted @ 2019-12-05 17:06  umbed  阅读(379)  评论(0编辑  收藏  举报