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

cyp*_*her 14 php smtp phpmailer smtp-auth

我正在尝试使用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.
Run Code Online (Sandbox Code Playgroud)

我的代码:

  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();
        }
Run Code Online (Sandbox Code Playgroud)

常数定义:

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');
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ale*_*exV 25

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

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

这意味着您发送的凭据将被SMTP服务器拒绝.确保主机,端口,用户名和密码良好.

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

$mail->SMTPSecure = 'tls';
Run Code Online (Sandbox Code Playgroud)

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

$mail->SMTPSecure = 'ssl';
Run Code Online (Sandbox Code Playgroud)

请记住:

  • 某些SMTP服务器可以禁止来自"局外人"的连接.
  • 某些SMTP服务器不支持SSL(或TLS)连接.

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

  • 您还可以在主机名前加上协议前缀(即 `$mail-&gt;Host = "tls://".SMTP_SERVER;`)并使用 `port 465` (2认同)