Use PHPMailer to send emails (HTML content, images, and attachments can be sent)

Source: Internet
Author: User
Tags php class

Phpmailer is a php class used to send emails. It is much better than the built-in function mail in php. phpmailer can be downloaded from the official website.
The following shows an example of sending only text.
*/

The code is as follows: Copy code

Require ("class. phpmailer. php ");
$ Mail = new phpmailer ();
$ Mail-> ismail ();

$ Mail-> addaddress ("email@example.com ");
$ Mail-> subject = "test 1 ";
$ Mail-> body = "test 1 of phpmailer .";

If (! $ Mail-> send ())
{
Echo "error sending:". $ mail-> errorinfo ;;
}
Else
{
Echo "letter sent ";
}
/*
$ Mail-> ismail (); required

Issendmail-via sendmail command.
Isqmail-directly via qmail mta.
Issmtp-via smtp server.
Here is an smtp sample. Let's assume that the smtp needs authorization. If in't nessesary, write only $ email> smtpauth = 0 ;. The number of servers to be used uses the semicolumn separator
*/

Require ("class. phpmailer. php ");
$ Mail = new phpmailer ();
$ Mail-> issmtp ();
$ Mail-> host = "smtp1.example.com; smtp2.example.com ";
$ Mail-> smtpauth = true;
$ Mail-> username = 'smtpusername ';
$ Mail-> password = 'smtppassword ';

$ Mail-> addaddress ("email@example.com ");
$ Mail-> subject = "test 1 ";
$ Mail-> body = "test 1 of phpmailer .";

If (! $ Mail-> send ())
{
Echo "error sending:". $ mail-> errorinfo ;;
}
Else
{
Echo "letter is sent ";
}

/*

To add sender inforation, use the following functions:

The code is as follows: Copy code

Mail-> from = "mailer@example.com ";
$ Mail-> fromname = "my site's mailer ";
$ Mail-> sender = "mailer@example.com"; // indicates returnpath header
$ Mail-> addreplyto ("replies@example.com", "replies for my site"); // indicates replyto headers

For specifying various types of specified ients use these:

$ Mail-> addaddress ("mail1@domain.com", "your ient 1 ");
$ Mail-> addcc ("mail1@domain.com", "your ient 1 ");
$ Mail-> addbcc ("mail1@domain.com", "your ient 1 ");


How to use garbled characters

$ Mail-> charset = "windows-1251 ";
$ Mail-> charset = "UTF-8 ";

Set encoding


If you want to send an email, you can send an image and add the following html code before $ mail-send ().

The code is as follows: Copy code
$ Mail-> ishtml (true );
$ Mail-> addembeddedimage('logo.jpg ', 'logomg', 'logo.jpg '); // attach file logo.jpg, and later link to it using identfier logoimg
$ Mail-> body = "<P> this is a test picture: </p> ";
$ Mail-> altbody = "this is text only alternative body .";


Send attachment

The code is as follows: Copy code

$ Mail-> ishtml (false );
$ Mail-> addattachment ('www .111cn.net/invoice-user-1234.pdf', 'invoice.pdf '); // attach files/invoice-user-1234.pdf,

*/

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.