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

发布于 2024-09-17 05:18:24 字数 2322 浏览 6 评论 0原文

我正在尝试使用 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.

我的代码:

  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();
        }

常量定义:

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');

有什么想法吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

若言繁花未落 2024-09-24 05:18:49

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

与 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;
}

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

works with gmail.com with authenticate:

<?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;
}
我的影子我的梦 2024-09-24 05:18:46

使用 one.com 作为 SMTP 端口 465 不起作用。我可以在 phpmailer 中使用此配置:

$mail->Host = 'send.one.com';
$mail->SMTPAuth = true;
$mail->Username = "***";
$mail->Password = "******";
$mail->From = "***";
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = 'none';
$mail->Port = 25;

Using one.com as SMTP port 465 wasn't working. I could using this configuration in phpmailer:

$mail->Host = 'send.one.com';
$mail->SMTPAuth = true;
$mail->Username = "***";
$mail->Password = "******";
$mail->From = "***";
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = 'none';
$mail->Port = 25;
梦醒灬来后我 2024-09-24 05:18:44

我在大约一个小时内遇到了非常类似的问题,直到我弄清楚出了什么问题。我的问题是,我使用了SSL,而不是ssl。检查代码中区分大小写AlexV 引导我找到了问题的根源。 helo/ehlo 的东西似乎无关紧要。

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.

墨洒年华 2024-09-24 05:18:43

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

这是尝试使用不使用 SSL 的客户端连接到 SSL 服务的典型情况

SMTP 错误:无法进行身份验证。

毫无疑问,无法启动 SMTP 对话
身份验证不是一个选项。

phpmailer 不执行隐式 SSL(又名连接时的 TLS、SMTPS)
如果没有重写 smtp.class.php 以包含对其的支持,就无法执行您所要求的操作。

请改用带有显式 SSL(又名 TLS、STARTTLS)的端口 587。

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

that's typical of trying to connect to a SSL service with a client that's not using SSL

SMTP Error: Could not authenticate.

no suprise there having failed to start an SMTP conversation
authentigation is not an option,.

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.

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

疯狂的代价 2024-09-24 05:18:41

是的,您需要 OpenSSL 才能正常工作。我的备份测试站点可以工作,但我的实时服务器却不能。不同之处?实时服务器在 PHP 配置中没有 OpenSSL。

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.

北笙凉宸 2024-09-24 05:18:39

这可能看起来像是盲目尝试,但如果 SMTP 需要 SSL,请确保 PHP 已符合 OpenSSL。

要检查使用 phpinfo()

希望它有帮助!

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

To check use phpinfo()

Hope it helps!

傾旎 2024-09-24 05:18:37

尝试端口 25 而不是 456。

我在使用端口 456 时遇到了同样的错误,并将其更改为 25 对我有用。

try port 25 instead of 456.

I got the same error when using port 456, and changing it to 25 worked for me.

何以畏孤独 2024-09-24 05:18:35

尝试通过该 SMTP 服务器手动/从交互式邮件程序(例如 Mozilla Thunderbird)发送电子邮件。从错误来看,服务器似乎不会接受您的凭据。该端口上运行的是 SMTP 还是 SSL+SMTP?您似乎没有在您发布的代码中使用安全连接,并且我不确定 PHPMailer 是否确实支持 SSL+SMTP。

(谷歌搜索 SMTP 服务器主机名的第一个结果: http://podpora.ebola.cz/idx.php/0/006/article/Strucny-technicky-popis-nastaveni-sluzeb.html似乎在说“SMTPs邮件发送:安全SSL连接,端口:465”。)

看来 PHPMailer 确实支持 SSL;至少来自这个。因此,您需要将其更改

define('SMTP_SERVER', 'smtp.ebola.cz');

为:

define('SMTP_SERVER', 'ssl://smtp.ebola.cz');

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.

(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" . )

It looks like PHPMailer does support SSL; at least from this. So, you'll need to change this:

define('SMTP_SERVER', 'smtp.ebola.cz');

into this:

define('SMTP_SERVER', 'ssl://smtp.ebola.cz');
两仪 2024-09-24 05:18:32

据我所知,您的代码一切正常。你的错误是:

SMTP 错误:无法进行身份验证。

这意味着 SMTP 服务器拒绝您发送的凭据。确保主机、端口、用户名和密码正确。

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

$mail->SMTPSecure = 'tls';

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

$mail->SMTPSecure = 'ssl';

请记住:

  • 某些 SMTP 服务器可以禁止来自“外部人员”的连接。
  • 某些 SMTP 服务器不支持 SSL(或 TLS)连接。

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

[来源]

As far as I can see everything is right with your code. Your error is:

SMTP Error: Could not authenticate.

Which means that the credentials you've sending are rejected by the SMTP server. Make sure the host, port, username and password are good.

If you want to use STARTTLS, try adding:

$mail->SMTPSecure = 'tls';

If you want to use SMTPS (SSL), try adding:

$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.

Maybe this example can help (GMail secure SMTP).

[Source]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文