php resource centre

  • about
  • articles
  • tutorials
  • resources
  • certification
Home

Primary links

  • About
  • Articles
  • Tutorials
  • Resources
  • Certification

Sending mail using SMTP authentication

admin — Thu, 25/03/2010 - 10:09am

Spammers are very active now a days and they are constantly searching for vulnerable scripts or servers to send spam emails. One common vulnerability in servers is where mails can be send as nobody user. Nowadays most server administrators block this and mails are allowed to be send as a valid user created in the server. When we use the mail() function in php to send emails, the emails get send as the user nobody. So this won;t work in all servers. You won't get any error in the script. Recipients won't get the emails. The if(mail(***,***,***)) will return false in such cases.

One good method to overcome this is to use SMTP authentication mode in the mailer script. The following script uses PEAR class for sending emails.

<?php
require_once "Mail.php";

$from = "You ";
$to = "The Recipient ";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "yoursmtpserver.com";

$username = "youremailusername";

$password = "yourpassword";

$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!

");
}

?>

  • Articles
  • Login to post comments

User login

  • Request new password

Follow Us

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Nisha
  • linnaeus
  • Yameen
  • TalleyReedy
  • admin

Follow vipin7873 on Twitter

<!-- Start of Woopra Code -->

<!-- End of Woopra Code -->

  • about
  • articles
  • tutorials
  • resources
  • certification

copyright © 2010 Vipin Chandran