Use the PHPMailer plug-in php to send emails through gmail

Source: Internet
Author: User
Tags gmail mail
Phpmailer is an excellent php third-party email sending class function, which supports almost all domestic and foreign email login and sending functions. let's take a look at the PHPMailer plug-in implementation in php.

Phpmailer is an excellent php third-party email sending function, which supports almost all domestic and foreign email login and sending functions. let's take a look at the example of using PHPMailer plug-in php to implement gmail mail sending, I hope this tutorial will be helpful to you.

PHPMailer is a mail sending class specifically used in php language. it has powerful functions and enriches PHP's single mail () function. Supports SMTP and attachments. PHPMailer complies with LGPL authorization and can be downloaded for free.

: Http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/PHPMailer_v5.1.zip/download

Use method (only mail is used, and its function of receiving mail is too popular): The code is as follows:

  1. IsSMTP ();
  2. $ Mail-> SMTPAuth = true; // authentication is required for smtp
  3. $ Mail-> SMTPSecure = 'SSL '; // smtp authentication method
  4. $ Mail-> Port = 465; // smtp Port number
  5. $ Mail-> Username = "username@gmail.com"; // registered gmail user mailbox (you can use your gmail mailbox)
  6. $ Mail-> Password = "password"; // Password
  7. $ Mail-> Host = 'smtp .gmail.com '; // The sending server.
  8. $ Mail-> IsHTML (true); // whether to enable HTML mail sending. the default value is false,
  9. $ Mail-> From = 'username @ gmail.com '; // The sender's email address. use your gmail email address.
  10. $ Mail-> FromName = 'mailer'; // your name
  11. $ Mail-> Subject = 'mail title'; // mail title
  12. $ Mail-> Body = 'Hello World'; // the email content. HTML code is supported.
  13. $ Mail-> AddAddress ('name @ anydomain.com ', 'First LA'); // recipient's email address and name
  14. If (! $ Mail-> Send ()){
  15. Echo $ mail-> ErrorInfo;
  16. }

Generally, emails can be sent through the specified smtp server according to the above configuration. However, after my personal test, the above settings can only send emails using 163,126 smtp, such as qq, gmail and so on cannot be sent! After searching for N on google for a long time, I finally found the following method to successfully send emails via gmail. of course, it also includes qq, sina, sohu, etc (Yahoo cannot pass, it seems that Yahoo has not activated smtp by default. you have to activate one of their mail reminders to activate smtp/pop3)

The following is a solution: you only need to find the following code in class. phpmailer. php (the downloaded version may be slightly different). The code is as follows:

  1. If (eregi ('^ (. +) :( [0-9] +) $', $ hosts [$ index], $ hostinfo )){
  2. $ Host = $ hostinfo [1];
  3. $ Port = $ hostinfo [2];
  4. } Else {
  5. $ Host = $ hosts [$ index];
  6. $ Port = $ this-> Port;
  7. }

Change to (any version is changed to the following content ):

  1. If (eregi ('^ # ([a-z] + ://)? [^:] +) :( D +) # I ', $ hosts [$ index], $ hostinfo )){
  2. $ Host = $ hostinfo [1];
  3. $ Port = $ hostinfo [3];
  4. } Else {
  5. $ Host = $ hosts [$ index];
  6. $ Port = $ this-> Port;
  7. }

After several modifications, I hope you can send emails smoothly through smtp in gmail (of course, you must be able to send emails under win, you have to install smtp in IIS and support OpenSSL extensions in php.) Finally, after testing, the modified smtp can successfully send emails through 126163qqgmailsinasohu.

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.