Howdy, Stranger!

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

Categories

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.