PHP sample code: Use the SMTP server of Gmail or Google Apps to send emails online

Source: Internet
Author: User
Tags php sample code

TIPS:

SMTP and POP3 In Gmail are both SSL-encrypted.

 

Step1. PHP OpenSSL module (Extension) Support

Step2. download phpmailer Library

Step3. change code 'class. phpmailer. php' and 'class. SMTP. php'

Add property is_ssl (VAR $ is_ssl = false;) to phpmailer and SMTP ;)

The smtpconnect method in phpmailer is passed to the SMTP object ($ this-> SMTP-> is_ssl = $ this-> is_ssl ;)

 

The Connect Method in SMTP is added before the fsockopen call.

If ($ this-> is_ssl) {$ host = 'ssl: // '. $ host ;}

 

Write an example to see if OK

 


$ Mail = new phpmailer ();

$ Mail-> issmtp ();

 

$ Mail-> encoding = $ this-> encoding;

 

$ Mail-> host = $ this-> ***; // SMTP servers

$ Mail-> smtpauth = true; // turn on SMTP authentication

 

$ Mail-> username = $ this-> ***; // SMTP Username

$ Mail-> Password = $ this-> ***; // SMTP Password

 

$ Mail-> from = $ this-> ***;

$ Mail-> fromname = $ this-> ***;

$ Mail-> addreplyto ($ this-> ***, $ this-> ***);

 

$ Mail-> charset = $ this-> encoding;

$ Mail-> encoding = "base64 ";

 

$ Mail-> ishtml (true); // send as HTML



$ Mail-> subject = $ this-> subject;

$ Mail-> body = $ this-> body;

 

$ Mail-> altbody = "text/html ";

 

$ Mail-> addaddress ($ to_email );

 

// $ Mail-> smtpdebug = true;

$ Mail-> is_ssl = true;

$ Mail-> Port = 465;

 

// Var_dump ($ mail );

// Exit;

 

$ Mail-> send ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.