0

I tried to send an email using php

I managed smtp as "smtp.gmail.com"

After this setting I get no further, receiving the foloowing SMTP error

From address failed:[email protected]

I put

$mail->SetFrom("[email protected]");

but still failing... I tried to change php.ini but it wasn't possible

I saved php.ini after changing (sendmail_path ) in the public location but no result

1 Answer 1

2

Try this instead:

$mail             = new PHPMailer();

$body             = "<h1>hello, world!</h1>"

$mail->IsSMTP(); // telling the class to use SMTP

$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   = "[email protected]";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "[email protected]";
$mail->AddAddress($address, "John Doe");

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

I think Google doesn't let you use their domain without meeting some conditions, but I think this will work. Code extracted from here: http://phpmailer.worxware.com/?pg=examplebgmail

EDIT
I used the following code exactly and it worked. (I had some trouble with two step authentication but I fixed it. You can use your regular password without worry. If you too have two step authentication enabled you should put there a one time password generated for your e-mail address)

<?php
    require("libphp-phpmailer/class.phpmailer.php");

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587;
    $mail->Username = "[email protected]";
    $mail->Password = "myOneTimePasswordBecauseOfTwoStepAuth";
    $mail->setFrom("[email protected]");
    $mail->AddAddress("[email protected]");
    $mail->Subject = "trial";
    $mail->Body = "<h1>hello, world!</h1>";

    if ($mail->Send() == false)
    {
        die($mail->ErrInfo);
    }
    else
    {
        echo "It worked!\n";
    }
?> 
17
  • thank you for your consideration .I still have the problem ... the error is as SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
    – mojtaba1
    Commented Jul 12, 2015 at 18:33
  • Could you add a little more code?
    – ChrisG
    Commented Jul 12, 2015 at 18:45
  • i exactly did your code .. the very same code .. but I got the error which I wrote previously
    – mojtaba1
    Commented Jul 12, 2015 at 19:11
  • 1
    Hi dear friend. your attention to my problem made me happy by being such nice guys who are ready to help the others even they don't know. even after updating the appliance ( update50) I didn't succeed and still it gives error.but the most important thing is not the answer but having friends like you who are always ready to help. as soon as I got the answer or what I had made wrong, I'll let you know. appreciated. good luck
    – mojtaba1
    Commented Jul 13, 2015 at 11:36
  • 1
    hey ChrisG, thanks mate ! It worked like a charm on the first attempt. Using it on my final project and it helped me a lot !
    – Gabriel
    Commented Dec 23, 2015 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .