This article mainly introduces to you how to solve the problem of PHPMailer failing to send emails under Linux server. The article introduces it in detail through sample code. I believe it has certain reference value for everyone. Friends who need it can read it together. Take a look.
Requirements
After changing the server, I found that my sending email function failed! The original server was fine, but there must be something wrong, so I decided to check it out. I use PHPMailer and SMTP to send emails.
Troubleshooting process
This method first requires PHP to enable sockets expansion. I checked the phpinfo page and found that it is enabled:
After checking, openssl is also turned on (because I took the qq mailbox to test), so there is no problem:
Then check again Check allow_url_fopen, turn it on, no problem:
Is the function disabled? Not disabled, no problem:
Then there is no problem with the configuration. I was wondering, is the port occupied?
Run it: netstat -tnlp
The first one is this:
Port 25 is occupied , occupied by something called master. Good guy, let’s see what it is. Run ps -f -p 1818
to see the results. 1818 is the process number PID of the current program. You can see that it is :
Postfix is running. It may have been installed accidentally when setting up the environment.
What is postfix?
Postfix is a free mail server running in a Linux environment, or MTA (Mail Transfer Agent). Others include Sendmail, Qmail, exim and Zmailer. So Postfix is a mail server. Then this thing must be in conflict. We need to request an external mail server through port 25, but a local mail server is running on port 25. This is probably not possible.
Try to solve the problem
Let’s try to use our mail server to send emails instead of using an external server (for example, we used Alibaba Cloud Enterprise Mail before). We put a small piece of test code into the same level of the PHPMailer directory:
<?php header("content-type:text/html;charset=utf-8"); require 'PHPMailer/class.phpmailer.php'; try { $mail = new PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; $mail->SMTPAuth = true; $mail->Port = 25; $mail->Host = '127.0.0.1';//邮箱smtp地址 $mail->Username = 'gzp@gzpblog.com';//你的邮箱账号 $mail->Password = '扒拉扒拉。。。';//你的邮箱密码 $mail->From = 'gzp@gzpblog.com';//你的邮箱账号 $mail->FromName = '锅子'; $to = "扒拉扒拉@qq.com"; $mail->AddAddress($to); $mail->Subject = "test"; $mail->Body = 'hello!'; $mail->WordWrap = 80; $mail->IsHTML(true); $mail->Send(); echo "success!"; } catch (phpmailerException $e) { echo "邮件发送失败:".$e->errorMessage(); }
Send an email through the local server on port 25, run this page, and find that it does not work. The error cannot be verified, indicating that there are still some configurations that need to be done, and it does not work for the time being. , instead of delving further into the local server, let’s try changing it back:
$mail->Host = ‘smtp.mxhichina.com'; //阿里云的邮箱smtp地址
Try it, but it still doesn’t work:
Cannot connect to SMTP. Then we kill the postfix server on port 25, execute kill 1818 (the PID of the current postfix), and execute it again. It still gets the same error and cannot connect. This is strange. There is no program running on port 25, so it still doesn't work.
Possible reasons
It is found that it may be due to ipv6. When phpMailer performs DNS resolution of the smtp server, it obtains the IP v6 address, and then compares it with the IP v6 The resolved address is used to connect, causing the connection to fail.
Let me try:
ip -6 addr show
Nothing, that’s not the problem .
What is the reason?
Solution to the problem
Since port 25 is not available, I thought if I could try other ports and try port 465.
Port 465 (SMTPS): Port 465 is open for the SMTPS (SMTP-over-SSL) protocol service. This is a variant of the SMTP protocol based on the SSL security protocol. It inherits SSL security. The high security and reliability of the protocol's asymmetric encryption can prevent email leakage. SMTPS, like the SMTP protocol, is also used to send emails, but it is more secure to prevent emails from being intercepted and leaked by hackers, and it can also implement the anti-repudiation function of the email sender. Prevents the sender from deleting the sent email after sending it and refusing to admit that such an email was sent.
465 port seems to be better, so I started to try it directly. Try the following. The following is the command:
sbin/iptables -I OUTPUT -p tcp –dport 465 -j ACCEPT
打通465端口
/etc/rc.d/init.d/iptables save
保存
service iptables restart
重启
/etc/init.d/iptables status
查看需要打开的端口是否生效?
似乎可行,现在尝试一下,用SMTP的465SSL连接方式来发送邮件,稍微改了一下测试代码:
<?php header("content-type:text/html;charset=utf-8"); require 'PHPMailer/class.phpmailer.php'; try { $mail = new PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->Host = 'smtp.mxhichina.com';//邮箱smtp地址 $mail->Username = 'gzp@gzpblog.com';//你的邮箱账号 $mail->Password = '扒拉扒拉。。。';//你的邮箱密码 $mail->From = 'gzp@gzpblog.com';//你的邮箱账号 $mail->FromName = '锅子'; $to = "扒拉扒拉@qq.com"; $mail->AddAddress($to); $mail->Subject = "test"; $mail->Body = 'hello!'; $mail->WordWrap = 80; //$mail->AddAttachment("f:/test.png"); //可以添加附件 $mail->IsHTML(true); $mail->Send(); echo "success!"; } catch (phpmailerException $e) { echo "邮件发送失败:".$e->errorMessage(); //测试的时候可以去掉此行的注释 }
执行,成功!右下角弹出了QQ邮件的提醒。
总结
PHPMailer通过465端口进行更安全的SMTPS协议发送邮件
可以修改:
$mail->Port = 465;
为:
$mail->SMTPSecure = 'ssl'; $mail->Port = 465;
即可。
以上就是Linux服务器下PHPMailer发送邮件失败的问题解决的技巧的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!