简体   繁体   中英

How to send email in PHP

I am experiencing the same problem as SMTP connect() failed PHPmailer - PHP . My code is as follows:

public function sendEmail($toAddress, $subject, $message)
{
    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'username';
    $mail->Password = 'password';
    $mail->SMTPSecure = 'tls';
    $mail->SMTPDebug = 2;
    $mail->Port = 587;
    $mail->setFrom('support@tripmatcher.com', 'Tripmatcher Support');
    $mail->addAddress($toAddress);
    $mail->Subject = $subject;
    $mail->Body = $message;
    if (!$mail->send()) {
        error_log('Mailer Error: ' . $mail->ErrorInfo);
    } else {
        error_log('Email has been sent.');
    }
}

Two questions:

  1. What is wrong here, as far as I can see my code looks correct, and I have also enabled less secure apps for my email address?
  2. [Mon Sep 18 21:01:43.342766 2017] [:error] [pid 22052] [client 2.28.76.119:55132] Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting , referer:

That is all I see in the logs, how can I produce a more descriptive error message? I have tried looking at similar questions, and I still can't work out what exactly the problem is.

对我来说,注释掉isSMTP()行使其可以工作,但是它仍然进入我的hotmail帐户的垃圾邮件文件夹中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM