Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to send emails using GMail SMTP server from a PHP page?

Jeson84Jeson84 USAPosts: 90Member

I need to send emails using GMail SMTP server in my custom PHP page. Please let me know how to do this.

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member

    Extracted from http://stackoverflow.com/a/2748837/2606686

    // [Pear Mail Library](http://pear.php.net/package/Mail/docs)
    require_once "Mail.php";

    $from = '<from.gmail.com>';
    $to = '<to.yahoo.com>';
    $subject = 'Hi!';
    $body = "Hi,\n\nHow are you?";

    $headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
    );

    $smtp = Mail::factory('smtp', array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => '465',
    'auth' => true,
    'username' => 'johndoe@gmail.com',
    'password' => 'passwordxxx'
    ));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
    } else {
    echo('<p>Message successfully sent!</p>');
    }
Sign In or Register to comment.