分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

PHPMailer 发送邮件(二)

发布时间:2023-09-06 01:23责任编辑:赖小花关键词:PHP邮件

  发现PHPMailer又做了较大的更新,以前发送邮件的方法已不太适用,刚好要做一个实验,需要搭建个环境,这里使用Gmail进行测试,现记录下来。

  传送地址Github: PHPMailer

  基本要求的内容跟之前的文章是一样的: phpmailer 发送邮件(一)   

一、基本要求

  • Web访问正常(apache可以正常访问)
  • php 设置正确(Sockets Support、OpenSSL support 两项设置为enable)
  • gmail设置, 允许不够安全的应用:设置为已启用

可以写一个简单的语句测试一下:info.php

<?phpphpinfo();?>

二、PHPmailer 

  新版的PHPMailer跟之前的最大不同在于需要使用composer, ubuntu 下composer的安装可参考:ubuntu 安装 php Composer

  安装完成之后,我们需要使用composer来下载PHPMailer。

  我们先切换到web跟目录,创建一个phpmailer的目录,切换到该目录,然后使用命令下载文件:composer require phpmailer/phpmailer, 这个过程会耗费点时间。

lz@starnight:/var/www/html$ pwd/var/www/htmllz@starnight:/var/www/html$ lshello.html ?index.html ?info.php ?phpmailerlz@starnight:/var/www/html$ cd phpmailer/lz@starnight:/var/www/html/phpmailer$ composer require phpmailer/phpmailer

  下载完成之后,我们可以看到目录下多了一些文件,我们创建一个新的文件mailer.php用来发送文件。

lz@starnight:/var/www/html/phpmailer$ pwd/var/www/html/phpmailerlz@starnight:/var/www/html/phpmailer$ lscomposer.json ?composer.lock ?mailer.php ?vendorlz@starnight:/var/www/html/phpmailer$ ls vendor/autoload.php ?composer ?phpmailer

  mailer.php的内容如下:

<?php// Import PHPMailer classes into the global namespace// These must be at the top of your script, not inside a functionuse PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;//Load composer‘s autoloaderrequire ‘vendor/autoload.php‘;$mail = new PHPMailer(true); ?????????????????????????????// Passing `true` enables exceptionstry { ???//Server settings ???$mail->SMTPDebug = 2; ????????????????????????????????// Enable verbose debug output ???$mail->isSMTP(); ?????????????????????????????????????// Set mailer to use SMTP ???$mail->Host = ‘smtp.gmail.com‘; ?// Specify main and backup SMTP servers ???$mail->SMTPAuth = true; ??????????????????????????????// Enable SMTP authentication ???$mail->Username = ‘starnightcyber@gmail.com‘; ????????????????// SMTP username ???$mail->Password = ‘your password‘; ??????????????????????????// SMTP password ???$mail->SMTPSecure = ‘tls‘; ???????????????????????????// Enable TLS encryption, `ssl` also accepted ???$mail->Port = 587; ???????????????????????????????????// TCP port to connect to ???//Recipients ???$mail->setFrom(‘starnightcyber@gmail.com‘, ‘starnightcyber‘); ???$mail->addAddress(‘starnight_cyber@foxmail.com‘); ??????????????// Name is optional ???$mail->addAddress(‘zl15@foxmail.com‘); ???//Attachments // ??$mail->addAttachment(‘/var/tmp/file.tar.gz‘); ????????// Add attachments // ??$mail->addAttachment(‘/tmp/image.jpg‘, ‘new.jpg‘); ???// Optional name ???//Content ???$mail->isHTML(true); ?????????????????????????????????// Set email format to HTML ???$mail->Subject = ‘Test mail.‘; ???$mail->Body ???= ‘Hello, this is a test mail using phpmailer‘; ???$mail->AltBody = ‘Hello, this is a test mail using phpmailer‘; ???$mail->send(); ???echo ‘Message has been sent‘;} catch (Exception $e) { ???echo ‘Message could not be sent.‘; ???echo ‘Mailer Error: ‘ . $mail->ErrorInfo;}?>

  访问站点:

  等待一会(可能会比较慢,取决于你的网络状况),我们可以看到邮件被成功的发送出去了,下面是发送邮件的日志信息和相关截图。

2017-11-08 03:37:47 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:47 CLIENT -> SERVER: EHLO 192.168.0.82017-11-08 03:37:47 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [210.45.123.80]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF82017-11-08 03:37:47 CLIENT -> SERVER: STARTTLS2017-11-08 03:37:47 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS2017-11-08 03:37:47 CLIENT -> SERVER: EHLO 192.168.0.82017-11-08 03:37:48 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [210.45.123.80]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF82017-11-08 03:37:48 CLIENT -> SERVER: AUTH LOGIN2017-11-08 03:37:48 SERVER -> CLIENT: 334 VXNlcm5hbWU62017-11-08 03:37:48 CLIENT -> SERVER: c3Rhcm5pZ2h0Y3liZXJAZ21haWwuY29t2017-11-08 03:37:48 SERVER -> CLIENT: 334 UGFzc3dvcmQ62017-11-08 03:37:48 CLIENT -> SERVER: MTIzQHdheW5lJmx6OTMj2017-11-08 03:37:49 SERVER -> CLIENT: 235 2.7.0 Accepted2017-11-08 03:37:49 CLIENT -> SERVER: MAIL FROM:<starnightcyber@gmail.com>2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.0 OK d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:49 CLIENT -> SERVER: RCPT TO:<starnight_cyber@foxmail.com>2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.5 OK d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:49 CLIENT -> SERVER: RCPT TO:<zl15@foxmail.com>2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.5 OK d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:49 CLIENT -> SERVER: DATA2017-11-08 03:37:50 SERVER -> CLIENT: 354 Go ahead d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:50 CLIENT -> SERVER: Date: Wed, 8 Nov 2017 11:37:42 +08002017-11-08 03:37:50 CLIENT -> SERVER: To: starnight_cyber@foxmail.com, zl15@foxmail.com2017-11-08 03:37:50 CLIENT -> SERVER: From: starnightcyber <starnightcyber@gmail.com>2017-11-08 03:37:50 CLIENT -> SERVER: Subject: Test mail.2017-11-08 03:37:50 CLIENT -> SERVER: Message-ID: <pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI@192.168.0.8>2017-11-08 03:37:50 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.1 (https://github.com/PHPMailer/PHPMailer)2017-11-08 03:37:50 CLIENT -> SERVER: MIME-Version: 1.02017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: multipart/alternative;2017-11-08 03:37:50 CLIENT -> SERVER: boundary="b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI"2017-11-08 03:37:50 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: This is a multi-part message in MIME format.2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI2017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: Hello, this is a test mail using phpmailer2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI2017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: Hello, this is a test mail using phpmailer2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI--2017-11-08 03:37:50 CLIENT -> SERVER: 2017-11-08 03:37:50 CLIENT -> SERVER: .2017-11-08 03:37:52 SERVER -> CLIENT: 250 2.0.0 OK 1510112272 d74sm5796226pfe.167 - gsmtp2017-11-08 03:37:52 CLIENT -> SERVER: QUIT2017-11-08 03:37:52 SERVER -> CLIENT: 221 2.0.0 closing connection d74sm5796226pfe.167 - gsmtpMessage has been sent
phpmailer send mail log

  可以看到邮件发送成功。

  另外,上面代码中给出的邮箱都是有效的邮箱地址,没有打码,只是为了让各位能更清楚的看到效果,请改成自己的邮箱,不然自己也看不到是否发送成功,别瞎几把给我发邮件^_^。 

PHPMailer 发送邮件(二)

原文地址:http://www.cnblogs.com/Hi-blog/p/7803293.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved