Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
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.



  • DavidMDavidM USAPosts: 342Member

    Extracted from

    // [Pear Mail Library](
    require_once "Mail.php";
    $from = '<>';
    $to = '<>';
    $subject = 'Hi!';
    $body = "Hi,\n\nHow are you?";
    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    $smtp = Mail::factory('smtp', array(
            'host' => 'ssl://',
            'port' => '465',
            'auth' => true,
            'username' => '',
            '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.