使用PHPMailer通过SMTP发送电子邮件

时间:2021-07-18 14:32:39

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', 'myEmail@myDomain.tld');
define('SMTP_PASSWORD', '***CENSORED***');

define('MAIL_ORDERS_ADDRESS', 'myEmail@myDomain.tld');
define('MAIL_ORDERS_NAME', 'My Name');

Any ideas?

什么好主意吗?

8 个解决方案

#1


25  

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 TLS, try adding:

如果您想使用TLS,请尝试添加:

$mail->SMTPSecure = 'tls';

If you want to use SSL, try adding:

如果您想使用SSL,请尝试添加:

$mail->SMTPSecure = 'ssl';

Keep in mind that:

记住:

  • Some SMTP servers can forbid connections from "outsiders".
  • 一些SMTP服务器可以禁止来自“外部”的连接。
  • Some SMTP servers don't support SSL (or TLS) connections.
  • 有些SMTP服务器不支持SSL(或TLS)连接。

Maybe this example can help (GMail secure SMTP).

也许这个示例可以帮助(GMail安全SMTP)。

#2


4  

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.

尝试通过SMTP服务器手动/从交互邮件发送电子邮件(例如Mozilla Thunderbird)。从错误来看,服务器似乎不接受您的凭据。是在端口上运行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.html seems to say "SMTPs mail sending: secure SSL connection,port: 465" . )

(google搜索SMTP服务器主机名的第一个结果:http://podpora.ebola.cz/idx.php/0/006/article/strucny -technicky- popastaveni -sluzeb。html似乎说“SMTPs邮件发送:安全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');

#3


2  

This may seem like a shot in the dark but make sure PHP has been complied with OpenSSL if SMTP requires SSL.

这看起来像是在暗中操作,但如果SMTP需要SSL,请确保PHP已被OpenSSL所遵守。

To check use phpinfo()

检查使用phpinfo()

Hope it helps!

希望它可以帮助!

#4


2  

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对我来说是可行的。

#5


1  

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才能正常工作。我的备份测试站点工作,而我的实时服务器没有工作。不同吗?Live server在PHP配置中没有OpenSSL。

#6


0  

SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:

SMTP ->来自服务器:SMTP ->来自服务器:SMTP ->错误:EHLO不接受服务器:

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除了重写smtp.class之外,不做隐式SSL(连接上的TLS, SMTPS)。php要包含对它的支持,它无法执行您的要求。

Use port 587 with explicit SSL (aka TLS, STARTTLS) instead.

使用带有显式SSL(即TLS, STARTTLS)的端口587。

#7


0  

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 sensitive in the code. AlexV guided me to the source of the problem. That helo/ehlo -stuff seems irrelevant.

我也遇到过类似的问题,大约有一个小时,直到我发现哪里出了问题。我的问题是,我使用的是SSL,而不是SSL。检查在代码中是区分大小写的。阿列克谢夫带我找到了问题的根源。那些西罗/西罗的东西似乎无关紧要。

#8


0  

Simple smtp client with php stream socket with tls/ssl smtp STARTTLS command: https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php

带有php流套接字的简单smtp客户端,带有tls/ssl smtp STARTTLS命令:https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php

works with gmail.com with authenticate:

与gmail.com合作,认证:

<?php
// Login email and password
$login = "your-email@cool.xx";
$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: <hello@cool.com>\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: <to-email@boome.com>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <to-email@boome.com>\r\nFrom:<zour-email@cool.xx\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;
}

#1


25  

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 TLS, try adding:

如果您想使用TLS,请尝试添加:

$mail->SMTPSecure = 'tls';

If you want to use SSL, try adding:

如果您想使用SSL,请尝试添加:

$mail->SMTPSecure = 'ssl';

Keep in mind that:

记住:

  • Some SMTP servers can forbid connections from "outsiders".
  • 一些SMTP服务器可以禁止来自“外部”的连接。
  • Some SMTP servers don't support SSL (or TLS) connections.
  • 有些SMTP服务器不支持SSL(或TLS)连接。

Maybe this example can help (GMail secure SMTP).

也许这个示例可以帮助(GMail安全SMTP)。

#2


4  

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.

尝试通过SMTP服务器手动/从交互邮件发送电子邮件(例如Mozilla Thunderbird)。从错误来看,服务器似乎不接受您的凭据。是在端口上运行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.html seems to say "SMTPs mail sending: secure SSL connection,port: 465" . )

(google搜索SMTP服务器主机名的第一个结果:http://podpora.ebola.cz/idx.php/0/006/article/strucny -technicky- popastaveni -sluzeb。html似乎说“SMTPs邮件发送:安全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');

#3


2  

This may seem like a shot in the dark but make sure PHP has been complied with OpenSSL if SMTP requires SSL.

这看起来像是在暗中操作,但如果SMTP需要SSL,请确保PHP已被OpenSSL所遵守。

To check use phpinfo()

检查使用phpinfo()

Hope it helps!

希望它可以帮助!

#4


2  

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对我来说是可行的。

#5


1  

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才能正常工作。我的备份测试站点工作,而我的实时服务器没有工作。不同吗?Live server在PHP配置中没有OpenSSL。

#6


0  

SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:

SMTP ->来自服务器:SMTP ->来自服务器:SMTP ->错误:EHLO不接受服务器:

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除了重写smtp.class之外,不做隐式SSL(连接上的TLS, SMTPS)。php要包含对它的支持,它无法执行您的要求。

Use port 587 with explicit SSL (aka TLS, STARTTLS) instead.

使用带有显式SSL(即TLS, STARTTLS)的端口587。

#7


0  

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 sensitive in the code. AlexV guided me to the source of the problem. That helo/ehlo -stuff seems irrelevant.

我也遇到过类似的问题,大约有一个小时,直到我发现哪里出了问题。我的问题是,我使用的是SSL,而不是SSL。检查在代码中是区分大小写的。阿列克谢夫带我找到了问题的根源。那些西罗/西罗的东西似乎无关紧要。

#8


0  

Simple smtp client with php stream socket with tls/ssl smtp STARTTLS command: https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php

带有php流套接字的简单smtp客户端,带有tls/ssl smtp STARTTLS命令:https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php

works with gmail.com with authenticate:

与gmail.com合作,认证:

<?php
// Login email and password
$login = "your-email@cool.xx";
$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: <hello@cool.com>\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: <to-email@boome.com>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <to-email@boome.com>\r\nFrom:<zour-email@cool.xx\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;
}