Use PHPMailer to send Gmail account email _ PHP Tutorial

Source: Internet
Author: User
Tags word wrap
Use PHPMailer to send emails with the Gmail account. The detailed code is as follows: Php code defines a function for sending emails, which has passed the test. $ Sendto_email: email sending address $ subject: Email subject $ body: the detailed code of the email body is as follows:


Php code
// The following defines a function for sending emails, which has passed the test.
// $ Sendto_email: email sending address
// $ Subject: email subject
// $ Body: body of the email
// $ Sendto_name indicates the sender's name. It is generally saved.
Function stmp_mail ($ sendto_email, $ subject = null, $ body = null, $ sendto_name = null ){

Vendor ("PHPMailer. class # phpmailer"); // class of the function package imported. phpmailer. php


$ Mail = new PHPMailer (); // creates a mail sending class object.
$ Mail-> IsSMTP (); // send via SMTP
$ Mail-> Port = 25; // sending Port
$ Mail-> Host = "ssl: // smtp.gmail.com: 465"; // SMTP mail server address, which must be replaced with the mail server address where the email is sent, SMTP settings of gmail are used here
$ Mail-> SMTPAuth = true; // turn on SMTP authentication
$ Mail-> Username = "leobrilliantlife@gmail.com"; // The user name of this mailbox on the SMTP server, some only need to @ the front part, and some need the full name. Replace it with the correct email user name
$ Mail-> Password = "*****"; // replace the Password of the mailbox on the SMTP server with the correct Password.
$ Mail-> From = "leobrilliantlife@gmail.com"; // The mailbox that sent this email on the SMTP server, replace it with the correct one, and the value of $ mail-> Username corresponds.
$ Mail-> FromName = "handheld Shunde"; // enter the real sender's name and other information as needed.
$ Mail-> CharSet = "UTF-8"; // The character set is specified here!
$ Mail-> Encoding = "base64 ";
$ Mail-> AddAddress ($ sendto_email, $ sendto_name); // recipient's email address and name
// $ Mail-> AddReplyTo ('sdaping @ mail.ustc.edu.cn ', "administrator"); // This item is set as needed
// $ Mail-> WordWrap = 50; // set word wrap
// $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // process attachments
// $ Mail-> AddAttachment ("/tmp/image.jpg", "new.jpg ");
$ Mail-> IsHTML (true); // send as HTML
$ Mail-> Subject = $ subject; // mail Subject
// Email content
$ Mail-> Body ="


'. $ Body .'";
$ Mail-> AltBody = "text/html ";
If (! $ Mail-> Send ()){
// Email sending failed
Return false;
} Else {
// The email is sent successfully.
Return true;
}

} // Function end

Author "butter"

Php code // The following defines a function for sending emails, which has passed the test. // $ Sendto_email: email sending address // $ subject: email subject // $ body: body content...

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.