Skip to content

Instantly share code, notes, and snippets.

@eytanbiala
Last active December 29, 2015 03:38
Show Gist options
  • Save eytanbiala/7608674 to your computer and use it in GitHub Desktop.
Save eytanbiala/7608674 to your computer and use it in GitHub Desktop.
PHP snippet to send email through SMTP
<?php
require_once "Mail.php";
$from = "Sender <sender@example.com>";
$to = "Recipient <recipient@gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "imap.gmail.com";
$username = "john.doe";
$password = "password123";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment