使用PHPMailer在我的主机上使用外部smtp

时间:2017-03-19 05:16:56

标签: email smtp phpmailer

我已经搜索过每个地方寻找解决此问题的方法,我找到了许多提示,但没有成功。我正在使用PHPMailer来使用外部smtp(gmail或sendpulse)。我已经尝试了所有可能的端口,想法......这是我使用的代码:

$<?php
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 6;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Username = "sender@gmail.com";
$mail->Password = "password";
$mail->setFrom('sender@gmail.com', 'fsender lsender');
$mail->addAddress('receiver@gmail.com', 'freceiver lreceiver');
$mail->Subject = 'PHPMailer SMTP test';
$mail->msgHTML(file_get_contents('mail.txt'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

调试:

Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()<br>
Connection: opened<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 00:57:26 -0400 &quot;<br>
SMTP -&gt; get_lines(): $data is &quot;220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 00:57:26 -0400 &quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;220-We do not authorize the use of this system to transport unsolicited, &quot;<br>
SMTP -&gt; get_lines(): $data is &quot;220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 00:57:26 -0400 220-We do not authorize the use of this system to transport unsolicited, &quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;220 and/or bulk e-mail.&quot;<br>
SERVER -&gt; CLIENT: 220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 00:57:26 -0400 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.<br>
CLIENT -&gt; SERVER: EHLO myaccount.arvixeshared.com<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-SIZE 52428800&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-8BITMIME&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-PIPELINING&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-AUTH PLAIN LOGIN&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-STARTTLS&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250 HELP&quot;<br>
SERVER -&gt; CLIENT: 250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP<br>
CLIENT -&gt; SERVER: STARTTLS<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;220 TLS go ahead&quot;<br>
SERVER -&gt; CLIENT: 220 TLS go ahead<br>
CLIENT -&gt; SERVER: EHLO myaccount.arvixeshared.com<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-SIZE 52428800&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-8BITMIME&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-PIPELINING&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250-AUTH PLAIN LOGIN&quot;<br>
SMTP -&gt; get_lines(): $data is &quot;250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;250 HELP&quot;<br>
SERVER -&gt; CLIENT: 250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [xx.xx.xx.x]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP<br>
Auth method requested: UNKNOWN<br>
Auth methods available on the server: PLAIN,LOGIN<br>
Auth method selected: LOGIN<br>
CLIENT -&gt; SERVER: AUTH LOGIN<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;334 XXXxxxxxxXXx&quot;<br>
SERVER -&gt; CLIENT: 334 XXXxxxxxxXXx<br>
CLIENT -&gt; SERVER: XXxxxxXxXXxxXXXxXXxxXXxxXxXxxX==<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;334 XXXxxxxxxxXx&quot;<br>
SERVER -&gt; CLIENT: 334 XXXxxxxxxxXx<br>
CLIENT -&gt; SERVER: YW5pczE5OTY=<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;535 Incorrect authentication data&quot;<br>
SERVER -&gt; CLIENT: 535 Incorrect authentication data<br>
SMTP ERROR: Password command failed: 535 Incorrect authentication data<br>
SMTP Error: Could not authenticate.<br>
CLIENT -&gt; SERVER: QUIT<br>
SMTP -&gt; get_lines(): $data is &quot;&quot;<br>
SMTP -&gt; get_lines(): $str is  &quot;221 myaccount.arvixeshared.com closing connection&quot;<br>
SERVER -&gt; CLIENT: 221 myaccount.arvixeshared.com closing connection<br>
Connection: closed<br>
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting<br>
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我确定密码是正确的,我在另一个帐户中尝试了这个代码并且工作正常。我联系了我的主机,但没有解决方案,我注意到的是SERVER - &gt;客户&amp; CLENT - &gt; SERVER是相同的(localhost):

SERVER -&gt; CLIENT: 220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 00:57:26 -0400 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.<br>
CLIENT -&gt; SERVER: EHLO myaccount.arvixeshared.com<br>

代码工作的帐户中的位置SERVER - &gt;客户(smtp.gmail.com)&amp;客户 - &gt; SERVER(localhost):

SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP x29xx4280503xxx.16 - gsmtp<br>
CLIENT -&gt; SERVER: EHLO myaccount.hawkhost.com<br>

有人可以帮我解决这个问题,谢谢。

更新: 激活证书验证并使用php 7.0

username@domain.com [~/path/e]# /usr/local/php70/bin/php -f gmail.php 
2017-03-19 12:58:06 Connection: opening to smtp.gmail.com:587, timeout=300, options=array (
                                      )
2017-03-19 12:58:06 Connection: opened
2017-03-19 12:58:06 SMTP -> get_lines(): $data is ""
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 08:58:06 -0400 
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 08:58:06 -0400 
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "220-We do not authorize the use of this system to transport unsolicited, 
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 08:58:06 -0400 
                                      220-We do not authorize the use of this system to transport unsolicited, 
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "220 and/or bulk e-mail.
                                      "
2017-03-19 12:58:06 SERVER -> CLIENT: 220-myaccount.arvixeshared.com ESMTP Exim 4.87 #1 Sun, 19 Mar 2017 08:58:06 -0400 
                                      220-We do not authorize the use of this system to transport unsolicited, 
                                      220 and/or bulk e-mail.
2017-03-19 12:58:06 CLIENT -> SERVER: EHLO myaccount.arvixeshared.com
2017-03-19 12:58:06 SMTP -> get_lines(): $data is ""
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-SIZE 52428800
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-8BITMIME
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-PIPELINING
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-AUTH PLAIN LOGIN
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      250-AUTH PLAIN LOGIN
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250-STARTTLS
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $data is "250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      250-AUTH PLAIN LOGIN
                                      250-STARTTLS
                                      "
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "250 HELP
                                      "
2017-03-19 12:58:06 SERVER -> CLIENT: 250-myaccount.arvixeshared.com Hello myaccount.arvixeshared.com [23.91.70.2]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      250-AUTH PLAIN LOGIN
                                      250-STARTTLS
                                      250 HELP
2017-03-19 12:58:06 CLIENT -> SERVER: STARTTLS
2017-03-19 12:58:06 SMTP -> get_lines(): $data is ""
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "220 TLS go ahead
                                      "
2017-03-19 12:58:06 SERVER -> CLIENT: 220 TLS go ahead
2017-03-19 12:58:06 Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=`*.arvixeshared.com' did not match expected CN=`smtp.gmail.com' [/home/username/path/p/class.smtp.php line 368]
2017-03-19 12:58:06 SMTP Error: Could not connect to SMTP host.
2017-03-19 12:58:06 CLIENT -> SERVER: QUIT
2017-03-19 12:58:06 SMTP -> get_lines(): $data is ""
2017-03-19 12:58:06 SMTP -> get_lines(): $str is  "221 myaccount.arvixeshared.com closing connection
                                      "
2017-03-19 12:58:06 SERVER -> CLIENT: 221 myaccount.arvixeshared.com closing connection
2017-03-19 12:58:06 Connection: closed
2017-03-19 12:58:06 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

2 个答案:

答案 0 :(得分:0)

您的ISP看起来正在拦截并将您的SMTP流量重定向到他们自己的服务器。您要求连接到smtp.gmail.com,但它已连接到myaccount.arvixieshared.com,当然您的凭据无效。

错误消息链接到的故障排除指南中对此进行了介绍。

它应该无法连接,因为TLS证书不匹配,因此您要么使用旧的PHP版本,要么已禁用证书验证,这两者都不是一个好主意。

答案 1 :(得分:0)

与Arvixe支持人员聊天4天后,结论如下: 如果它发生并且你遇到问题就不要打扰自己,就离开吧。 @Synchro,再次感谢您的帮助和建议,我没有听取它(获得更好的ISP)。 这是我与他们的最后一次聊天:

3:20:07 PM 
System 
Hitesh N has joined the chat! 
3:20:31 PM 
aissam 
My Support Code:

XXXXXX-XXXXX 
3:20:44 PM 
Hitesh N 
Hello Aissam. Welcome to Arvixe live chat, My name is Hitesh. How may I assist you? 
3:22:08 PM 
aissam 
for the fourth day, could you please fix my problem and please don't say I will pass it to my supervisor or administrator, because you don't have such a thing 
3:22:18 PM 
aissam 
03/19/17 [dallas134.arvixeshared.com]PHp mailer fucntion issue XXX-XXX-XXXXX 
3:22:25 PM 
Hitesh N 
Could I place you on hold for 3-4 minutes while I check this for you? 
3:22:54 PM 
aissam 
Okay 
3:34:42 PM 
Hitesh N 
I am still working on your issue. 
3:40:32 PM 
aissam 
Okay take your time 
3:53:15 PM 
Hitesh N 
May I know when the issue started? 
3:55:26 PM 
aissam 
before about one week 
4:03:56 PM 
Hitesh N 
I am still working on your issue. Sorry for delay. 
4:10:43 PM 
Hitesh N 
Sorry for long delay. 
4:11:00 PM 
Hitesh N 
My tech confirmed that your issue was already escalated the Sysadmins to check further. 
4:11:12 PM 
Hitesh N 
But as there are huge number of tickets, its getting delayed. 
4:11:49 PM 
Hitesh N 
Now, we have again requested them to check further and update you. 
4:12:00 PM 
Hitesh N 
I know we have delayed a lot, sorry for that. 
4:13:46 PM 
aissam 
please don't tell that, smtp is essential, my platform is using it for sending email, now I'm just receiving I can't send, and if I send the messages goes through your smtp server and they are all classified spam 
4:14:04 PM 
aissam 
this is not an issue that need more than one day to be resolved 
4:14:29 PM 
aissam 
why your server is redirect me to localhost and doesn't allow me to connect to external smtp? 
4:15:25 PM 
Hitesh N 
Unfortunately, I don't know the reason behind it as of now. 
4:15:41 PM 
Hitesh N 
Our sysadmins have to check the logs to get more info on it. 
4:16:23 PM 
Hitesh N 
I don't know if its possible, as I don't know about the scripts much. 
4:16:33 PM 
aissam 
move me to another server unit the virtual admin or supervisor that previous three agents told me about will check the issue perhaps in 2030 
4:16:50 PM 
Hitesh N 
But normally for emails sent which are going to spam box, we suggest to add spf record. Is that possible in your scripts? 
4:17:18 PM 
aissam 
I've added spf the same thing always to spam folder 
4:17:28 PM 
aissam 
which scripts you're talking about? 
4:17:51 PM 
Hitesh N 
I meant in your PHP mail function. 
4:18:46 PM 
aissam 
my problem isn't with script, how you can solve my problem if you don't understand it. the three idiot before told that they passed all information to ticket, I think they didn't 
4:19:26 PM 
Hitesh N 
All have passed the ticket to sysadmins but as there are already ticket in sysadmins pool, its taking time. 
4:21:46 PM 
aissam 
my problem isn't with script, my problem is that your server doesn't allow me to connect to external smtp, and keep redirect me to your smtp server when I'm asking to connect to smtp.gmail.com or another smtp server. please update my problem, I don't have problems with scripts, they are just for testing purpose to debug the problem (I've used PHPMail, SwiftMail, PHP+Pear), I'm helping you in debug of problem and you're saying that I have problem with script? 
4:22:09 PM 
Hitesh N 
No, I didn't mean that Aissam. I didn't. 
4:22:26 PM 
Hitesh N 
As I said, I don't know much about the issue, I just referred about adding spf record. 
4:22:37 PM 
Hitesh N 
But as you mentioned, spf record is already present. 
4:24:04 PM 
aissam 
This is very well known problem about emails sending, if you need to be professional use external smtp (special on that domain) and specifically don't use smtp of shared account 
4:24:40 PM 
aissam 
and it's not a problem with you smtp server it's for all ISP 
4:24:48 PM 
Hitesh N 
I have updating all of your inputs in ticket for our sysadmins review. It will be helpful. 
4:25:34 PM 
aissam 
Thank you for your help, but whereis this admin. Am I the only one who have this issue in dallas134 
4:25:45 PM 
aissam 
Is it a global issue? 
4:26:27 PM 
Hitesh N 
I'm not sure Aissam, because as of now we have not received any report of email failure on server: dallas134.arvixeshared.com 
4:27:05 PM 
Hitesh N 
I will ask with my tech, a moment. 
4:27:30 PM 
aissam 
so the problem is only within my account, also I have another problem with it. I can't buy anything until i contact you 
4:27:50 PM 
aissam 
when it's time to generate the invoice I get an error "enter email address" 
4:28:30 PM 
Hitesh N 
I'm sorry for that. 
4:29:37 PM 
aissam 
you don't have an account for testing purpose in this server? 
4:30:04 PM 
Hitesh N 
No. 
4:30:54 PM 
aissam 
what tech said about dallas134? 
4:31:32 PM 
Hitesh N 
There is no known issue on email functionality on server. 
4:32:44 PM 
aissam 
I know that, I can send email using your smtp without problem, ask them about why they are blocking connection to external smtp? 
4:36:47 PM 
Hitesh N 
They confirmed that there is no blocking of connection to external smtp. 
4:36:58 PM 
Hitesh N 
Regarding your issue, we need to check further. 
4:39:30 PM 
aissam 
Honestly, since December 2016, You couldn't fix simple issue which is related to generation of invoices in my account, and whenever I need to buy a domain or simple thing I need to contact you to generate the invoice on behalf of me. how you could solve this complicated issue? 
4:40:06 PM 
aissam 
You have two solutions, to keep ignoring me like you're doing know, or to move me to another account 
4:41:18 PM 
Hitesh N 
The invoice issue is being worked upon from many time as we got update that ISP is also a reason in it. 
4:41:23 PM 
Hitesh N 
Its still ongoing. 
4:41:44 PM 
Hitesh N 
Regarding the issue, we will surely help you. 
4:43:05 PM 
aissam 
When? 2017 or 2030? 
4:45:30 PM 
Hitesh N 
We will surely help you asap.