How to Send Email from a PHP Script Using SMTP Authentication

Now you can send emails with SMTP authentication using this script. 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.

Send Email from a PHP Script Using SMTP Authentication

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

* Make sure the PEAR Mail package is installed. (PEAR Mail install in Ubuntu)
* Make sure you change the following variables in given example:

  1. from: the email address from which you want the message tbe sent.
  2. to: the recipient’s email address and name.
  3. host: your outgoing SMTP server name.
  4. username: the SMTP user name (typically the same as the user name used tretrieve mail).
  5. password: the password for SMTP authentication.

Sending Mail from PHP Using SMTP Authentication:

<?php

require_once “Mail.php”;

/* mail setup recipients, subject etc */
$recipients = “Recipient Name “;
$headers[“From”] = “Sender Name “;
$headers[“To”] = “Recipient Name “;
$headers[“Subject”] = “Hi”;
$mailmsg = “Hello,nnThis is a test.”;

/* SMTP server name, port, user/passwd */
$smtpinfo[“host”] = “mail.openshell.in”;
$smtpinfo[“port”] = “26”;
$smtpinfo[“auth”] = true;
$smtpinfo[“username”] = “smtp_username”;
$smtpinfo[“password”] = ‘smtp_password’;

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory(“smtp”, $smtpinfo);
/* Ok send mail */

$mail = $mail_object->send($recipients, $headers, $mailmsg);

if (PEAR::isError($mail)) {

echo “<p>”. $mail->getMessage() .”</p>”;
} else {

echo “<p>Message successfully sent!</p>”;
}
?>

Sending Mail from PHP Using SMTP Authentication and SSL Encryption:

<?php

require_once “Mail.php”;

/* mail setup recipients, subject etc */
$recipients = “Recipient Name “;
$headers[“From”] = “Sender Name “;
$headers[“To”] = “Recipient Name “;
$headers[“Subject”] = “Hi”;
$mailmsg = “Hello,nnThis is a test.”;

/* SMTP server name, port, user/passwd */
$smtpinfo[“host”] = “ssl://mail.openshell.in”;
$smtpinfo[“port”] = “465”;
$smtpinfo[“auth”] = true;
$smtpinfo[“username”] = “smtp_username”;
$smtpinfo[“password”] = ‘smtp_password’;

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory(“smtp”, $smtpinfo);
/* Ok send mail */

$mail = $mail_object->send($recipients, $headers, $mailmsg);

if (PEAR::isError($mail)) {

echo “<p>”. $mail->getMessage() .”</p>”;
} else {

echo “<p>Message successfully sent!</p>”;
}
?>

Permanent link to this article: https://blog.openshell.in/2011/01/how-to-send-email-from-a-php-script-using-smtp-authentication/

Leave a Reply

Your email address will not be published.