php 使用 PHPMailer 通过 SMTP 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3618712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending emails through SMTP with PHPMailer
提问by cypher
I'm trying to send SMTP e-mails using PHPMailer, but I keep getting this error message, any ideas how to get rid of it?
I'm trying to connect via SSL on port 465.
我正在尝试使用 PHPMailer 发送 SMTP 电子邮件,但我不断收到此错误消息,有什么想法可以摆脱它吗?
我正在尝试通过端口 465 上的 SSL 进行连接。
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:
Notice: fputs() [function.fputs]: send of 18 bytes failed with errno=32 Roura p?eru?ena (SIGPIPE) in /home/www/amazonek.cz/subdomains/library/PHPMailer_v5.1/class.smtp.php on line 494
SMTP -> FROM SERVER:
SMTP -> ERROR: HELO not accepted from server:
Notice: fputs() [function.fputs]: send of 12 bytes failed with errno=32 Roura p?eru?ena (SIGPIPE) in /home/www/amazonek.cz/subdomains/library/PHPMailer_v5.1/class.smtp.php on line 212
SMTP -> ERROR: AUTH not accepted from server:
SMTP Error: Could not authenticate.
My code:
我的代码:
require_once('../library/PHPMailer_v5.1/class.phpmailer.php');
try{
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = SMTP_SERVER;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPDebug = 2;
$mail->SetFrom(MAIL_ORDERS_ADDRESS, MAIL_ORDERS_NAME);
$mail->Subject = 'AMAZONEK.cz - objednávka ?íslo '.$_SESSION['orderId'];
$mail->MsgHTML('<b>Ahoj</b>');
$mail->AddAddress($_SESSION['user']['email'], $_SESSION['user']['name'].' '.$_SESSION['user']['surname']);
$mail->AddBCC(MAIL_ORDERS_ADDRESS, MAIL_ORDERS_NAME);
if(!$mail->Send()) throw new Exception($mail->ErrorInfo);
}
catch(Exception $e){
echo $e->getMessage();
}
Constants definition:
常量定义:
define('SMTP_SERVER', 'smtp.ebola.cz');
define('SMTP_PORT', 465);
define('SMTP_USERNAME', '[email protected]');
define('SMTP_PASSWORD', '***CENSORED***');
define('MAIL_ORDERS_ADDRESS', '[email protected]');
define('MAIL_ORDERS_NAME', 'My Name');
Any ideas?
有任何想法吗?
回答by AlexV
As far as I can see everything is right with your code. Your error is:
据我所知,您的代码一切正常。你的错误是:
SMTP Error: Could not authenticate.
SMTP 错误:无法验证。
Which means that the credentials you've sending are rejected by the SMTP server. Make sure the host, port, username and password are good.
这意味着您发送的凭据被 SMTP 服务器拒绝。确保主机、端口、用户名和密码正确。
If you want to use STARTTLS, try adding:
如果要使用 STARTTLS,请尝试添加:
$mail->SMTPSecure = 'tls';
If you want to use SMTPS (SSL), try adding:
如果要使用 SMTPS (SSL),请尝试添加:
$mail->SMTPSecure = 'ssl';
Keep in mind that:
请记住:
- Some SMTP servers can forbid connections from "outsiders".
- Some SMTP servers don't support SSL (or TLS) connections.
- 某些 SMTP 服务器可以禁止来自“外人”的连接。
- 某些 SMTP 服务器不支持 SSL(或 TLS)连接。
Maybe this examplecan help (GMail secure SMTP).
也许这个例子可以帮助(GMail 安全 SMTP)。
回答by Piskvor left the building
Try to send an e-mail through that SMTP server manually/from an interactive mailer (e.g. Mozilla Thunderbird). From the errors, it seems the server won't accept your credentials. Is that SMTP running on the port, or is it SSL+SMTP? You don't seem to be using secure connection in the code you've posted, and I'm not sure if PHPMailer actually supports SSL+SMTP.
尝试手动/从交互式邮件程序(例如 Mozilla Thunderbird)通过该 SMTP 服务器发送电子邮件。从错误来看,服务器似乎不会接受您的凭据。该 SMTP 是在端口上运行,还是 SSL+SMTP?您似乎没有在您发布的代码中使用安全连接,而且我不确定 PHPMailer 是否真的支持 SSL+SMTP。
(First result of googling your SMTP server's hostname: http://podpora.ebola.cz/idx.php/0/006/article/Strucny-technicky-popis-nastaveni-sluzeb.htmlseems to say "SMTPs mail sending: secure SSL connection,port: 465" . )
(谷歌搜索您的 SMTP 服务器的主机名的第一个结果:http: //podpora.ebola.cz/idx.php/0/006/article/Strucny-technicky-popis-nastaveni-sluzeb.html似乎说“SMTP 邮件发送:安全SSL 连接,端口:465"。)
It looks like PHPMailer does support SSL; at least from this. So, you'll need to change this:
看起来 PHPMailer 确实支持 SSL;至少从这一点。所以,你需要改变这个:
define('SMTP_SERVER', 'smtp.ebola.cz');
into this:
进入这个:
define('SMTP_SERVER', 'ssl://smtp.ebola.cz');
回答by Ludo - Off the record
try port 25 instead of 456.
尝试使用端口 25 而不是 456。
I got the same error when using port 456, and changing it to 25 worked for me.
我在使用端口 456 时遇到了同样的错误,将其更改为 25 对我有用。
回答by Mikey1980
This may seem like a shot in the darkbut make sure PHP has been complied with OpenSSL if SMTP requires SSL.
这可能看起来像是在黑暗中拍摄,但如果 SMTP 需要 SSL,请确保 PHP 已与 OpenSSL 兼容。
To check use phpinfo()
检查使用 phpinfo()
Hope it helps!
希望能帮助到你!
回答by Todd
Yes, you need OpenSSL to work correctly. My backup testing site worked, my live server didn't. Difference? Live server didn't have OpenSSL within PHP configuration.
是的,您需要 OpenSSL 才能正常工作。我的备份测试站点工作,我的实时服务器没有。区别?实时服务器在 PHP 配置中没有 OpenSSL。
回答by user340140
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accept from server:
that's typical of trying to connect to a SSL service with a client that's not using SSL
这是尝试使用不使用 SSL 的客户端连接到 SSL 服务的典型情况
SMTP Error: Could not authenticate.
SMTP 错误:无法验证。
no suprise there having failed to start an SMTP conversation authentigation is not an option,.
毫不奇怪,无法启动 SMTP 对话身份验证不是一种选择。
phpmailer doesn't do implicit SSL (aka TLS on connect, SMTPS) Short of rewriting smtp.class.php to include support for it there it no way to do what you ask.
phpmailer 不执行隐式 SSL(又名 TLS 连接,SMTPS) 缺少重写 smtp.class.php 以包含对它的支持,它无法按照您的要求执行。
Use port 587 with explicit SSL (aka TLS, STARTTLS) instead.
改为使用带有显式 SSL(又名 TLS、STARTTLS)的端口 587。
回答by F-3000
I had very similar problem for something like an hour, until I figured out what went wrong. My problem was, that I used SSL, instead of ssl. Check is case sensitivein the code. AlexVguided me to the source of the problem. That helo/ehlo -stuff seems irrelevant.
我在一个小时内遇到了非常类似的问题,直到我弄清楚出了什么问题。我的问题是,我使用的是SSL,而不是ssl。检查在代码中区分大小写。 AlexV引导我找到了问题的根源。那个 helo/ehlo -stuff 似乎无关紧要。
回答by Max
Simple smtp client with php stream socket with tls/ssl smtp STARTTLS command: https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php
带有 tls/ssl smtp STARTTLS 命令的 php 流套接字的简单 smtp 客户端:https: //github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php
works with gmail.com with authenticate:
与 gmail.com 一起使用身份验证:
<?php
// Login email and password
$login = "[email protected]";
$pass = "123456";
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
// echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$socket) {
print "Failed to connect $err $errstr\n";
return;
}else{
// Http
// fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
// Smtp
echo fread($socket,8192);
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
// Start tls connection
echo fwrite($socket, "STARTTLS\r\n");
echo fread($socket,8192);
echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
// Send ehlo
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
// echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
// echo fread($socket,8192);
echo fwrite($socket, "AUTH LOGIN\r\n");
echo fread($socket,8192);
echo fwrite($socket, base64_encode($login)."\r\n");
echo fread($socket,8192);
echo fwrite($socket, base64_encode($pass)."\r\n");
echo fread($socket,8192);
echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
echo fread($socket,8192);
echo fwrite($socket, "DATA\n");
echo fread($socket,8192);
echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
echo fread($socket,8192);
echo fwrite($socket, "QUIT \n");
echo fread($socket,8192);
/* Turn off encryption for the rest */
// stream_socket_enable_crypto($fp, false);
fclose($socket);
}
}catch(Exception $e){
echo $e;
}