Archive for the ‘email will to to spam’ Tag

How to Send Email from a PHP Script Using SMTP Authentication

PHP mail() and SMTP Authentication

We are getting problem in sending email from our server and then we find this amazing resource, we are very thankful to this blog and content..

PHP mail() function is so simple is its lack of flexibility. Most importantly mail() does not usually allow you to use the SMTP server of your choice, and it does not support SMTP authentication, required by many a mail server today, at all.

Fortunately, overcoming PHP’s built-in shortcomings need not be difficult, complicated or painful either. For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too. For enhanced security, secure SSL connections are supported.

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:

Sending Mail from PHP Using SMTP Authentication – Example

require_once “Mails.php”;

$from = “Sandeep Verma “;
$to = “Sandeep Nayak “;
$subject = “Hi!”;
$body = “Hi,\n\nHow are you?”;

$host = “mail.sandeep.com”;
$username = “smtp_username”;
$password = “smtp_password”;

$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(”

” . $mail->getMessage() . ”

“);
} else {
echo(”

Message successfully sent!

“);
}
?>

Sending Mail from PHP Using SMTP Authentication and SSL Encryption – Example

require_once “Mails.php”;

$from = “SandeepVerma “;
$to = “My Recipient “;
$subject = “Hi!”;
$body = “Hi,\n\nHow are you?”;

$host = “ssl://mail.example.com”;
$port = “465”;
$username = “smtp_username”;
$password = “smtp_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 successfully sent!

“);
}
?>

$wgSMTP = array(
‘host’ => “ssl://smtp.gmail.com”,
‘IDHost’ => “gmail.com”,
‘port’ => 465,
‘auth’ => true,
‘username’ => “youremail@gmail.com”,
‘password’ => “yourpassword”
);

Reference: How to Send Email from a PHP Script Using SMTP Authentication – About Email