Tag: Mail

Send email using PHP, PEAR and Gmail

Posted by – 26/03/2010

Time to blog.

I have to implement a small web application which contains email system function. This is an example how to send an email with PHP and Gmail.

 
require_once 'Mail.php';
 
$from = "Forename Surname <example@googlemail.com>" //sender email;
$mailTo = "example@gmail.com";  //destination
$subject = "Send this mail from PHP!"; //email subject
$body = "Hello,\n\n How are you?"; //email body
$host = "smtp.gmail.com"; //Outgoing Mail (SMTP) Server
$port = "587"; //Set port
$username = "example@gmail.com"; //sender account
$password = "password"; //sender password
 
$headers = array ('From' => $from,
  				  'To' => $mailTo,
 				  'Subject' =>  $subject);
   				   $smtp = Mail::factory('smtp',
				   array ('host' =>  $host,
				   'port' =>  $port,
				   'auth' =>  true,
				   'username' =>  $username,
				   'password' =>  $password)); 
 
$mail = $smtp->send($to, $headers, $body); //call send method

Resources:

http://pear.php.net/manual/en/package.mail.mail.factory.php

http://mail.google.com/support/bin/answer.py?hl=en&answer=13287

Erawat.