Your problem starts here:
$mail->Username = “myemail@gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example@gmail.com', 'Nicolas El Mir');
You see those curly/smart quotes? “ ” ‘
“myemail...
“my gmail password”
‘example...
They're choking your script.
$mail->Username = "myemail@gmail.com";
$mail->Password = "my gmail password";
//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'Nicolas El Mir');
Had you error reporting set to catch/display, it would have told you about it.
I'm really hoping that is your actual code too, rather than just a bad Word Processor paste.
Plus, make sure you do have a Webserver/PHP installed and that mail is enabled on it.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Plus, pulled from this answer https://stackoverflow.com/a/16048485/ which may be the same problem for you, the port number, etc.
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
This code above has been tested and worked for me.
It could be that you needed $mail->SMTPSecure = 'ssl';
Also make sure you don't have two step verification switched on for that account as that can cause problems also.
UPDATED
You could try changing $mail->SMTP to:
$mail->SMTPSecure = 'tls';
It's worth noting that some SMTP servers block connections.
Some SMTP servers don't support SSL
(or TLS
) connections.
and pulled from another answer https://stackoverflow.com/a/31194724/ in that question:
The solution was that I had to remove this line:
$mail->isSMTP();