Announcement

Collapse
No announcement yet.

How to Send Email from a PHP Script Using SMTP Authentication

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Send Email from a PHP Script Using SMTP Authentication

    How to 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 through it ,you need to do following steps:

    Steps:
    1) Make sure the PEAR Mail package is installed.

    2)Adapt the example below for your needs. Make sure you change the following variables at least:
    a)From: the email address from which you want the message to be sent.
    b)To: the recipient's email address and name.
    c)Host: your outgoing SMTP server name.
    d)Username: the SMTP user name (typically the same as the user name used to retrieve mail).
    e)Password: the password for SMTP authentication.

    Example: Sending Mail from PHP Using SMTP Authentication

    <?php
    require_once "Mail.php";

    $from = "Sandra Sender <[email protected]>";
    $to = "Ramona Recipient <[email protected]>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "mail.example.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("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>

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

    <?php
    require_once "Mail.php";

    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $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("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>

    ---------------------------


    Regards,
    WHUK-Spartan

  • #2
    Thank you so much Spartan, was looking for a test script for so long but couldnt find any1 which wud work. This worked, finally my newsletters would roll out tonight. THanks again.
    Only I can live forever.

    Comment

    Working...
    X