尝试发送邮件时,PHP邮件程序在php 7中引发错误

时间:2018-07-11 09:17:20

标签: phpmailer php-7.0

我正在尝试使用PHP Mailer发送邮件,但出现错误。 请检查我的代码,让我知道解决方法。

这是我的代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer;

try {
    //Server settings
   $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'xxxxxxxxxxxx ';
    $mail->Password = 'xxxxxxxxx';
    //$mail->SMTPSecure = 'tls';                          // Enable TLS encryption, `ssl` also accepted
    $mail->SMTPSecure = 'ssl';                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xxxxxxx', 'shiva');

    $mail->addAddress('siva.sing.sivan@gmail.com', 'SP');     // Add a recipient
    $mail->addAddress('senthil.mca2008@gmail.com', 'SK');     // Add a recipient
    //\\$mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('opensourcesivaprakash@gmail.com', 'Information');


    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Test mail form SP';
    $mail->Body    = 'This is the HTML message  <b>From SP!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    }else{
        echo 'Message has been sent';
    }


} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

我的错误: 无法发送消息。邮件程序错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

请更正我的代码以解决此问题。

2 个答案:

答案 0 :(得分:2)

嘿,@ shiva使用此$mail->SMTPSecure = 'tls'并将端口用作$mail->Port = 587

告诉我它对您有用,请确保在您的帐户设置中启用“ 精简安全应用”,谢谢!

让我知道它对您有用吗?

答案 1 :(得分:0)

将php版本升级到7.2.x,并尝试使用内置的openssl 1.1.1