PHPMailer を使用して gmail メールを送信しようとしています。この投稿をフォローしました
これを行うために、以下に示す関数を設定しました。
function sendEmail($email, $name) {
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP I commented it cos it gives an error
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = 'email@gmail.com'; // Changed my email
$mail->Password = "password";// Changed my password
$mail->From = 'email@gmail.com';
$mail->FromName = 'FROM NAME';
$mail->AddAddress($email);
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$mail->Body = "Body";
if (!$mail->Send()) {
return false;
} else {
return true;
}
}
残念ながら、false を返し続けます。コードの何が問題なのか教えてください。
編集:私が得ているエラーを以下に示します:
SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
SMTP Error: Could not connect to SMTP host.
更新されたコード:
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port
$Mail->Username = EMAIL; // SMTP account username
$Mail->Password = PASS; // SMTP account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'SUB';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = EMAIL;
$Mail->FromName = 'FROM NAME';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress($email); // To:
$Mail->isHTML(TRUE);
$Mail->Body = "Hi";
$Mail->AltBody = "Hi";
$Mail->Send();
$Mail->SmtpClose();