Sending email is often a necessary feature in web applications. Here’s how you can send email from PHP by using SMTP through Gmail.
First, you’re going to need two PEAR packages: Mail and Net_SMTP. Make sure the directory where these files are found is listed in the include_path directive in your php.ini file.
This is based largely on a code snippet from email.about.com. Modify according to your needs. Entries in bold are ones you will definitely need to change.
require_once(‘Mail.php’);
$from = ‘Sender <sender@gmail.com>‘;
$to = ‘Receiver <receiver@something.com>‘;
$subject = ‘Sent from PHP on my machine‘;
$body = ‘This is a message I sent from PHP using the ‘
. ‘PEAR Mail package and SMTP through Gmail. Enjoy!‘;
$host = ‘smtp.gmail.com’;
$port = 587; //According to Google you need to use 465 or 587
$username = ‘sender‘;
$password = ‘your_password‘;
$headers = array(‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array(
‘host’ => $host,
‘port’ => $port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo “Message sent successfully!”;
}
echo “\n”;
?>
Pingback: How to use pear mail mime | DEEP in PHP