0

i want to send email after user checkout from cart

my controller:

include('js/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->Host     = "ssl://smtp.gmail.com"; // SMTP server Gmail 
$mail->Mailer   = "smtp";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPDebug = 1;
$mail->Username = "my gmail"; // 
$mail->Password = "my pass"; // SMTP password
$webmaster_email = "my gmail"; //Reply to this email ID
$email = "recipient gmail"; // Recipients email ID
$name = "John"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Aryono King";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Goeboek I-Mut");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject Test";
$mail->Body = "Test Content"; //HTML Body
if(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;}
else {echo "<strong>Email Send</strong>";}

but it show error like this

2015-05-20 21:46:43 SMTP ERROR: Failed to connect to server: (0) 2015-05-20 21:46:43 SMTP connect() failed. Mailer Error: SMTP connect() failed.

what's the problem? i can't solve it, i search everywhere and i can't fint the answer, please someone help me

yudijohn
  • 1,248
  • 3
  • 19
  • 43
  • Shouldn't the host be just `smtp.gmail.com` ? See if this helps: http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page – Maximus2012 May 20 '15 at 21:52
  • `->host` is literally just the DNS hostname. it's NOT a url. – Marc B May 20 '15 at 21:52
  • 1
    i change to smtp.gmail.com but its still get error – yudijohn May 20 '15 at 21:54
  • Try the solution here maybe: http://stackoverflow.com/questions/16044304/error-sending-e-mail-using-gmail-smtp ? – Maximus2012 May 20 '15 at 21:55
  • i remove $mail->Mailer = "smtp"; and pop up message email send, but no email come to inbox – yudijohn May 20 '15 at 22:00
  • try to write $mail->Host = "smpt.gmail.com:465" the debug Say 2015-05-20 22:05:30 Connection: opening to ssl://smtp.gmail.com:465, t=300, opt=array ( ) 2015-05-20 22:05:31 SMTP ERROR: Failed to connect to server: (0) 2015-05-20 22:05:31 SMTP connect() failed. Mailer Error: SMTP connect() failed. – yudijohn May 20 '15 at 22:05

2 Answers2

0

Two things you need to check before using phpmailer for gmail

  1. need to check whether SMTP port and email + password is correct or not

  2. You need to authenticate from your gmail account for sending email from unsecured sources, means: when first you send an email, google will send you an email asking your permission to allow to forward email from your id, here your required to click allow

One more thing, Port 587 is working perfectly for me, instead of 465

Hi,

there's more...I have seen that antivirus or firewall installed in your computer may block sending emails via localhost as it may considered as spam attacks

use this code...

include 'PHPMailerAutoload.php';
function send_mail($mail_to,$mail_to_fname,$mail_to_lname,$subject,$message)
{   $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->Debugoutput = 'html';
    $mail->Host = 'mail.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = " YOUR GMAIL ";
    $mail->Password = " YOUR GMAIL PASSWORD";
    $mail->setFrom(' YOUR GMAIL ', ' YOUR NAME ');
    $mail->addAddress($mail_to,$mail_to_fname . " " . $mail_to_lname);
    $mail->Subject = $subject;
    $mail->msgHTML($message);
    $mail->AltBody = ' ';
    if (!$mail->send()){echo "FALSE" . $mail->ErrorInfo;}
    else{echo "TRUE";}
}

// How to user
// send_email(" email address where to send "," reciepient first name ","reciepient last name "," subject ", " message as html code ");

you need to do one more step for allowing gmail to send messages...! (*) accept to allow google to send emails from unsecured apps, that link will be sent to your gmail, after you send first email

Marmik Bhatt
  • 607
  • 4
  • 16
  • 1
    i have do script like above with and without ssl:// but it still error, i try many smtp port like 587 an 465 and also may smtp host like mail.gmail.com, smtp.gmail.com, smtp.googlemail.com, but everything gone error – yudijohn May 21 '15 at 03:39
  • 1
    You also required to check that you have class.smtp.php file in same folder as PHPMailerAutoload.php, so that all three files class.phpmailer.php, class.smtp.php and PHPMailerAutoload.php in same folder or current path must be mentioned... – Marmik Bhatt Jun 22 '15 at 07:56
0

Take a look at the example here.

While personally, I do it like this:

    //Create a new PHPMailer instance
    $mail = new PHPMailer;

    //Tell PHPMailer to use SMTP
    $mail->isSMTP();

    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = 'ssl://smtp.googlemail.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = $email_mail; // SMTP username
    $mail->Password = $email_pass; // SMTP password
    $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465; // TCP port to connect to

Try to check against yours, like for example: $mail->Host = 'ssl://smtp.googlemail.com';, which supposed to be don't have ssl://.

Vainglory07
  • 5,073
  • 10
  • 43
  • 77