0

I have tried my best to recieve message on a gmail account using phpmailer but i can't. It shows message sent successfully but i don't recieve any message. I even edited the sendmail.ini file. This is the code:

use PHPMailer\PHPMailer\PHPMailer;    
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);   

//$mail->SMTPDebug = 2;           
//$mail->isSMTP();           
$mail->Host = 'smtp.gmail.com';    
$mail->SMTPAuth = true;          
$mail->Username = 'myemail@gmail.com';              
$mail->Password = 'mypassword';     
$mail->SMTPSecure = 'ssl';     
$mail->Port = 587;          
$mail->setFrom('myemail@gmail.com', 'Mailer');
$mail->addAddress('example@gmail.com', 'Joe User');     

$mail->isHTML(true);                
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';

if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

I no so many questions concerning this have been answered but i still need help

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
chinedu
  • 17
  • 4
  • 2
    Which of the pages on StackOverflow have you read? This way we don't point you to places you've already been. This IS a duplicate. – mickmackusa Feb 14 '18 at 11:15
  • 1
    Possible duplicate of [send email using Gmail SMTP server through PHP Mailer](https://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) – Datacrawler Feb 14 '18 at 11:17
  • most i saw was about SMTP CONNECT() FAILURE. Mine no error is shown but i dont get message in my account – chinedu Feb 14 '18 at 11:56
  • Most of your settings are ignored because you commented out `isSMTP`, but the combination of `SMTPSecure = 'ssl'` and `Port = 587` will not work, as all the examples provided with PHPMailer show, as do the troubleshooting guide and many questions on here. Because you're sending through your local mail server, any errors will be in your mail server log, also exactly as the docs say. – Synchro Feb 14 '18 at 12:13
  • @Synchro this was what i got 2018-02-15 09:16:52 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – chinedu Feb 15 '18 at 09:22
  • Right, so read the docs about how to tell whether your ISP is blocking outbound SMTP, which looks like your problem... – Synchro Feb 15 '18 at 09:25
  • Possible duplicate of [PHP on GoDaddy Linux Shared trying to send through GMAIL SMTP](https://stackoverflow.com/questions/5440026/php-on-godaddy-linux-shared-trying-to-send-through-gmail-smtp) – Synchro Feb 15 '18 at 09:30

3 Answers3

0

try:

$mail->SMTPSecure = 'tls';     
$mail->Port = 587;  

Hope it will help.

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
Nasir Khan
  • 19
  • 2
0

Change setFrom to SetFrom and try to change IP to 465. You can also try the following.

$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$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 'Message has been sent successfully';
} else {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

If you still get error than provide the error message.

Yogendra
  • 1,208
  • 5
  • 18
  • 38
-1

First of all, try to uncomment line

$mail->isSMTP();

If you send mail via https, try to add following code:

$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) );

Lotos
  • 231
  • 3
  • 10
  • mine isnt showing any error message. I get a success message but i go to my gmail and see nothing – chinedu Feb 14 '18 at 11:50
  • Never suggest compromising security without a good and explicit reason. This advice is just dangerous. – Synchro Feb 14 '18 at 12:10