PHP Send Mail using SMTP

  • Avinash
  • 7
  • Aug 09, 2011
  • Web Development

This tutorial shows you how you can send email using SMTP server’s settings. PHP people generally uses the PHP mail() function to send the mail.

But we can use the other SMTP servers to send the mail from our PHP script.

I will recommodend to use PEAR’s mail package to send the mail using SMTP.

Please refer the below code to send the mails using PHP and SMTP.

PS: Below code assumes that you have correctly included the PEAR and PEAR Mail files.

$subject = "This mail is sent from SMTP."; $mail_body = "This is the body of the mail which is sent using SMTP."; $from = "From: From Name "; $to = "To: To Name "; $receiver = "[email protected]"; // Setting up the headers $headers["From"] = $from; $headers["To"] = $to; $headers["Subject"] = $subject; $headers["Reply-To"] = "[email protected]"; $headers["Content-Type"] = "text/plain; charset=ISO-2022-JP"; $headers["Return-path"] = "[email protected]"; // Setting up the SMTP setting $smtp_info["host"] = "smtp.server.com"; $smtp_info["port"] = "25"; $smtp_info["auth"] = true; $smtp_info["username"] = "smtp_user"; $smtp_info["password"] = "smtp_password"; // Creating the PEAR mail object : $mail_obj =& Mail::factory("smtp", $smtp_info); // Sending the mail now $mail_sent = $mail_obj->send($receiver, $headers, $mail_body); // If any error the see for that here: if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}
$subject = "This mail is sent from SMTP.";
$mail_body = "This is the body of the mail which is sent using SMTP.";
$from = "From: From Name <[email protected]>";
$to = "To: To Name <[email protected]>";
$receiver = "[email protected]";

// Setting up the headers
$headers["From"] = $from;
$headers["To"] = $to;
$headers["Subject"] = $subject;
$headers["Reply-To"] = "[email protected]";
$headers["Content-Type"] = "text/plain; charset=ISO-2022-JP";
$headers["Return-path"] = "[email protected]";

// Setting up the SMTP setting
$smtp_info["host"] = "smtp.server.com";
$smtp_info["port"] = "25";
$smtp_info["auth"] = true;
$smtp_info["username"] = "smtp_user";
$smtp_info["password"] = "smtp_password";

// Creating the PEAR mail object :
$mail_obj =& Mail::factory("smtp", $smtp_info);

// Sending the mail now
$mail_sent = $mail_obj->send($receiver, $headers, $mail_body);

// If any error the see for that here:
if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}

You can download the PHP PEAR Package from here.

Note: It is recommodend to use PEAR’s Mail Package to send the mail using SMTP.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://opensourcexpert.com Naitra

    can we send mail via SMTP without Pear mail package?

    I think php mailer class also fine to send mail via smtp.

    PHP mailer class does not need any extra library to include.

    Yes, you are right with Pear Package because it is most powerful and widely used package in PHP development.

    • http://www.xpertdeveloper.com Avinash

      yes we can use the PHP Mailer also.. thanks for sharing this. Will post about it soon.

  • Fernando Rueda

    Hallo Bro …

    I hve a question … i like use mail() becouse is simple, fast (please dont took tome about other function or class) and until now i did not had any problem with it.

    But i am debelomet a intranet for some enterprice and they are still using “Outlook Express”, they dont wanna change it and Outlook always ask me for permision to see the image’s that come with tha normal mail.

    There’s any way to jump this and just show the image’s in a normal way ??

    I tired to try this (for a while) ….. Thanx for the Help.

    • http://www.xpertdeveloper.com Avinash

      Hi Frenando,

      I am not saying that mail() function has a problem. But I am just showing the alternative for sending the mail using PHP.
      And regarding the outlook image problem, I think that is for the security reasons but not sure about that.

      Thanks

  • Pingback: Send Mail via SMTP using PHPMailer | Expert PHP Developer

  • http://www.latinunit.net Jose David Garcia LLanos

    Hiya, I am getting the following error;

    Fatal error: Class 'mail' not found in /mounted-storage/home11b/sub001/sc16665-WHBS/auto-sal.es/functions.php on line 17
    Fatal error: Class 'mail' not found in /mounted-storage/home11b/sub001/sc16665-WHBS/auto-sal.es/functions.php on line 17
  • http://www.latinunit.net Jose David Garcia LLanos

    My host appears to have all the right modules installed

    https://www.servage.net/wiki/List_of_PEAR_modules