Send email using Gmail and PHP

This is really useful if you don’t have an email server.

Using your Gmail account for sending emails is great ;)

Steps:

  • Dowload the latest version of PHPmailer class
  • Include it in your script
  • Test with this code:
$mail  = new PHPMailer();
$mail->IsSMTP();
 
//GMAIL config
	$mail->SMTPAuth   = true;                  // enable SMTP authentication
	$mail->SMTPSecure = "ssl";                 // sets the prefix to the server
	$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
	$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
	$mail->Username   = "gmailusername";  // GMAIL username
	$mail->Password   = "gmailpassword";            // GMAIL password
//End Gmail
 
$mail->From       = "from@email.com";
$mail->FromName   = "you name";
$mail->Subject    = "some subject";
$mail->MsgHTML("the message");
 
	//$mail->AddReplyTo("reply@email.com","reply name");//they answer here, optional
$mail->AddAddress("address@to.com","name to");
$mail->IsHTML(true); // send as HTML
 
if(!$mail->Send()) {//to see if we return a message or a value bolean
  echo "Mailer Error: " . $mail->ErrorInfo;
} else  echo "Message sent!";
Related Posts Related Websites
Help sharing and Flatter me ;)

3 Comments

  1. Avinash Singh says:

    I used the code but i got the following error. what could be the probable reason. the internet connection is up and running while i am using this script..Please help..

  2. Avinash Singh says:

    Error:
    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

  3. Chema says:

    Maybe a problem with you hosting, port closed or something like that

Leave a Reply

Follow me