Web Development

PHP Send Mail using SMTP

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.

[cc lang=”php”]
$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());}
[/cc]

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.

Shares:
  • Naitra
    August 9, 2011 at 4:13 pm

    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.

    Reply
    • Avinash
      August 9, 2011 at 4:19 pm

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

      Reply
  • Fernando Rueda
    Fernando Rueda
    August 10, 2011 at 3:36 am

    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.

    Reply
    • Avinash
      August 10, 2011 at 9:16 am

      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

      Reply
  • […] Earlier post was about send mail using SMTP with PEAR::Mail package. […]

    Reply
  • 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

    Reply
  • My host appears to have all the right modules installed

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *