SMTP错误:无法连接到php中的SMTP主机

时间:2018-01-16 18:02:31

标签: php email smtp phpmailer

通过SMTP从Gmail发送电子邮件时出现此错误:

  

SMTP错误:无法连接到SMTP主机

我试过了:

require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPDebug  = 0;                     
$mail->SMTPAuth   = true;                  
$mail->SMTPSecure = "ssl";                 
$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 465;             
$mail->AddAddress($email);
$mail->Username="mail@gmail.com";  
$mail->Password="password";            
$mail->SetFrom('mail@gmail.com','Store');
$mail->AddReplyTo("mail@gmail.com","Store");
$mail->Subject    = $subject;
$mail->MsgHTML($message);
$mail->Send();

"较不安全的应用"的Gmail帐户设置是turned on

更新

尝试此answer后,我得到了:

  

致命错误:Class' SMTP'在第1522行的C:\ wamp64 \ www \ project \ mailer \ class.phpmailer.php中找不到

1 个答案:

答案 0 :(得分:-1)

我使用下面的代码设置我的值..它对我来说很好。

        $mail = new PHPMailer;
        //Tell PHPMailer to use SMTP
        $mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPSecure = 'tls'; 
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = "mail@gmail.com";
        //Password to use for SMTP authentication
        $mail->Password = "password";

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

        $mail->setFrom("mail@gmail.com",$from);
        $mail->addAddress("mail@gmail.com");
        //Set the subject line
        $mail->Subject = "subject";
        $mail->Body = "body";
        $mail->send();