Send mails via SMTP with auth

From D3xt3r01.tk
Revision as of 11:29, 24 February 2010 by Admin (talk | contribs) (New page: ==WHY== Because mail() doesn't always give the right results, also one might want to use it's own SMTP server ! ==HOW== Using PEAR's Mail class ! <source lang="php"> <?php require_once...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

WHY

Because mail() doesn't always give the right results, also one might want to use it's own SMTP server !

HOW

Using PEAR's Mail class !

<?php
require_once "Mail.php";
$from = "Name<mail@here.com>"; // This is how some use to forge the "From" Header. The Name isn't really required, an just set mail@here.com only without the <> part !
$to = "Name<destination@here.com>"; // Name isn't really required. You can just set destination@here.com only without the <> part !
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

// Server auth info
$host = "smtp.gmail.com";
$username = "auth@user.com";
$password = "p4ssw0rdg03sh3r3";

$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>");
}
?>