简体   繁体   中英

php mailer could not send email

I am trying to send an email from localhost with PHP. Here is my code:

<?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";
?>

When I run this code, it shows, "message has been sent" but it does not actually send the message. What is my problem?

change this code you have:

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

to this:

$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

In addition to what Lelio Faieta posted, enable less secure apps in gmail

and to prevent your mail going to your spam change

$mail->From = $email;

to your email and you can put $email in body of your message

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