文章目录
php发送邮件-亲测成功案例操作步骤如果openssl,没有开启记得,记得重启下iss应用程序池及iis服务器php发送邮件-亲测成功案例
php发送邮件-亲测成功案例
我的环境
服务器:iis
php:php7.2.5
操作系统:windows
操作步骤
先准备好线上能php的iis服务器
准备好QQ邮箱
开户这个,截图错了
准备好sendMail.zip解压到某个目录(window发送邮箱需要这个exe程序)
1. sendMail解压后的sendMail.ini需要配置成如下内容
sendMail.zip
; configuration for fake sendmail; if this file doesn't exist, sendmail.exe will look for the settings in; the registry, under HKLM\Software\Sendmail[sendmail]; you must change to your smtp server,; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup); emails delivered via IIS's pickup directory cause sendmail to; run quicker, but you won't get error messages back to the calling; application.smtp_server=; smtp port (normally 25)smtp_port=25; SMTPS (SSL) support; auto = use SSL for port 465, otherwise try to use TLS; ssl = alway use SSL; tls = always use TLS; none = never try to use SSLsmtp_ssl=auto; the default domain for this server will be read from the registry; this will be appended to email addresses when one isn't provided; if you want to override the value in the registry, uncomment and modify;default_domain=; log smtp errors to error.log (defaults to same directory as sendmail.exe); uncomment to enable loggingerror_logfile=error.log; create debug log as debug.log (defaults to same directory as sendmail.exe); uncomment to enable debugging;debug_logfile=debug.log; if your smtp server requires authentication, modify the following two linesauth_username=25472690xx@#必填的,发邮箱的邮箱auth_password=uwjdbfilqildexxx#必填的,发邮箱的授权码; if your smtp server uses pop3 before smtp authentication, modify the ; following three lines. do not enable unless it is required.pop3_server=pop3_username=pop3_password=; force the sender to always be the following email address; this will only affect the "MAIL FROM" command, it won't modify ; the "From: " header of the message contentforce_sender=; force the sender to always be the following email address; this will only affect the "RCTP TO" command, it won't modify ; the "To: " header of the message contentforce_recipient=; sendmail will use your hostname and your default_domain in the ehlo/helo; smtp greeting. you can manually set the ehlo/helo name if requiredhostname=
在php文件中编写脚本phpinfo()1. 查看php.ini所在目录
1. 如果没有此文件,则就在这目录创建此文件
2. 文件内容如下
extension=php_openssl.dll#开户openssl,否则发送邮件会报Extension missing: openssl[mail function]; For Win32 only.; /smtpSMTP = #我这里使用的是qq邮箱服务器; /smtp-portsmtp_port = 25; For Win32 only.; /sendmail-fromsendmail_from = 25472690xx@#发送的邮箱; For Unix only. You may supply arguments as well (default: "sendmail -t -i").; /sendmail-pathsendmail_path ="F:\Download\sendmail -t -i"#sendmail的exe路径mail.add_x_header = On
3. 查看是否开户open_ssl
下载别人写的发送邮箱的工具类,关键就下面这2个(PHPMailer.php,SMTP.php)链接
6. 我的代码```php<?php/*to: 邮件接收地址subject: 邮件主题body: 邮件内容attachpath:附件地址cc: 邮件抄送地址bcc: 邮件暗抄送地址*/function send_mail($to, $subject="", $body="", $attachpath="", $cc="", $bcc=""){// 对邮件内容进行必要的过滤//$body = eregi_replace("[\]",'',$body);// 设定时区date_default_timezone_set("PRC");require_once('PHPMailer.php');require_once("SMTP.php");// 实例化PHPMailer对象$mail = new PHPMailer();// 设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置为 UTF-8$mail->CharSet ="UTF-8";// 设定使用SMTP服务$mail->IsSMTP();// 启用 SMTP 验证功能$mail->SMTPAuth = true;// SMTP 安全协议$mail->SMTPSecure = 'ssl';$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;// SMTP 服务器$mail->Host = "";// SMTP服务器的端口号$mail->Port = 465;// SMTP服务器用户名和密码$mail->Username = "25472690xx@";$mail->Password = "uwjdbfilqildexxx";#qq邮箱要开户上图中的功能// 设置发件人地址和名称,名称可有可无$mail->SetFrom("25472690xx@", "25472690xx");$mail->AddAddress("670392xxx@");// 设置邮件接收地址和名称,第二个参数无所谓。必须用AddAddress添加邮件接收地址。AddReplyTo方法没什么用。//$mail->AddReplyTo("xxxxxx@", "xxxxxx");/*$mailaddrs = split(",", $to);foreach ($mailaddrs as $addres){//校验邮箱地址是否合法if (filter_var($addres, FILTER_VALIDATE_EMAIL)){$mail->AddAddress($addres);}}*//*// 设置邮件抄送地址if ($cc != ""){$ccaddrs = split(",", $cc);foreach ($ccaddrs as $ccaddr){//校验邮箱地址是否合法if (filter_var($ccaddr, FILTER_VALIDATE_EMAIL)){$mail->addCC($ccaddr);}}}*/// 设置邮件暗抄送地址,私密发送,我这个php版本要要报错,所以注释掉/*if ($bcc != ""){$bccaddrs = split(",", $bcc);foreach ($bccaddrs as $bccaddr){//校验邮箱地址是否合法if (filter_var($bccaddr, FILTER_VALIDATE_EMAIL)){$mail->addBCC($bccaddr);}}}*/// 设置邮件主题$mail->Subject = $subject;// 可选项,向下兼容考虑$mail->AltBody = "为了查看该邮件,请切换到支持 HTML 的邮件客户端";// 设置邮件内容$mail->MsgHTML($body);//使用HTML格式发送邮件$mail->IsHTML(true);// 添加附件,第一个参数是附件地址,第二个参数附件名//$mail->AddAttachment("images/phpmailer.gif");$mail->AddAttachment($attachpath);// 发送邮件if(!$mail->Send()){echo "发送失败:" . $mail->ErrorInfo . PHP_EOL;}else{echo "恭喜,邮件发送成功!" . PHP_EOL;}}$emailAddr = "670392xxx@";#收件人邮箱send_mail($emailAddr, "测试邮件", "<h1>使用PHPMailer类发送的邮件。</h1>", "ax.png", "670392xxx@", "");phpinfo();?>
如果openssl,没有开启记得,记得重启下iss应用程序池及iis服务器
我是手动创建了这个目录,手动把php模块中的dll文件复制到这目录中的,不然开不启openssl
成功截图
如果觉得《php发送邮件-亲测成功案例》对你有帮助,请点赞、收藏,并留下你的观点哦!