Use phpmailer to send emails in php

Source: Internet
Author: User
Php provides a function for sending emails. it can send emails directly in the program, however, it requires the server to support sendmail or to set up an email sending server that does not require relay, but now it is almost necessary to find an email sending relay that does not require authentication... "> <LINKhref ="

The mail function is provided in php. it can send emails directly in the program, but it requires the server to support sendmail.
Or you must set up an email sending server that does not require relay, but it is almost impossible to find an email sending relay that does not require authentication.
Therefore, emails cannot be sent successfully using the mail function.

If you are familiar with the SMTP protocol and use the socket function, you can write an efficient and stable mail sending program, but it is too difficult for general users. Fortunately, there are already many email sending modules compiled by others on the internet. we just need to download the module and call it easily, which is very convenient.

Here, we recommend a very powerful, easy-to-use and free SMTP class module-PHPMailer,

PHPMailer is a PHP function package used to send emails. It provides the following functions:
* Specify multiple recipients, CC addresses, hidden addresses, and reply addresses when sending mail.
* Multiple email codes are supported, including 8bit, base64, binary, and quoted-printable.
*. Supports SMTP Authentication.
*. Support for redundant SMTP servers
*. Supports attachments and Html-format emails.
*. Customize the mail header
* Images can be embedded in emails.
* Flexible debugging
*. Compatible SMTP servers include Sendmail, qmail, Postfix, Imail, and Exchange.
*. Can run on any platform

After downloading this component, write the code as follows to enable php to send emails online.

I. html program

Phpmailer Unit Test
Enter the email address for receiving the email:

II. PHP program

Require ("class. phpmailer. php"); // the downloaded file must be in the directory where the file is located.
$ Mail = new PHPMailer (); // Create a mail sending class
$ Address = $ _ POST ['address'];
$ Mail-> IsSMTP (); // send using SMTP
$ Mail-> Host = ""; // your enterprise Post Office domain name
$ Mail-> SMTPAuth = true; // enable SMTP verification
$ Mail-> Username = ""; // Post Office Username (enter the complete email address)
$ Mail-> Password = "******"; // Post Office Password
$ Mail-> From = ""; // email address of the sender
$ Mail-> FromName = "Your Name ";
$ Mail-> AddAddress ("$ address", ""); // The recipient address, which can be replaced with any email address in the format of AddAddress ("recipient email ", "recipient name ")
// $ Mail-> AddReplyTo ("","");
// $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // add an attachment
// $ Mail-> IsHTML (true); // set email format to HTML // whether HTML format is used
$ Mail-> Subject = "PHPMailer test mail"; // mail title
$ Mail-> Body = "Hello, this is the test mail"; // The mail content
$ Mail-> AltBody = "This is the body in plain text for non-HTML mail clients"; // additional information, which can be omitted
If (! $ Mail-> Send ())
Echo "failed to send the email.

Echo "error cause:". $ mail-> ErrorInfo;
Echo "email sent successfully ";

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: 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.