php邮件无法发送电子邮件

时间:2015-05-29 09:58:35

标签: php email

我正在尝试使用PHP从localhost发送电子邮件。这是我的代码:

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';

$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = false;     // turn on SMTP authentication

$mail->Username = "TTTTTT@gmail.com";  // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;   
$mail->SMTPSecure = '';  

$mail->From = $email;

$mail->AddAddress("bradm@inmotiontesting.com", "Brad Markle");

$mail->WordWrap = 50;

$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

当我运行此代码时,它显示“已发送消息”但它实际上并未发送消息。我的问题是什么?

2 个答案:

答案 0 :(得分:0)

更改此代码:

$mail->Username = "TTTTTT@gmail.com";  // SMTP username
$mail->Password = ""; // SMTP password
$mail->Port = 25;   
$mail->SMTPSecure = '';

到此:

$mail->SMTPAuth   = true;                  // enable SMTP authentication

$mail->SMTPSecure = "tls";                 // sets the prefix to the servier

$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server

$mail->Port       = 587;                   // set the SMTP port for the GMAIL server

$mail->Username   = "yourusername@gmail.com";  // GMAIL username

$mail->Password   = "yourpassword";            // GMAIL password

答案 1 :(得分:0)

除了Lelio Faieta发布的内容,enable less secure apps in gmail

并防止您的邮件发送到您的垃圾邮件 变化

$mail->From = $email;

到您的电子邮件中,您可以在邮件正文中添加$ email