Php two ways to send mail php use smtp to send mail

Source: Internet
Author: User
Tags mail code
: This article mainly introduces two methods for sending mails by php using smtp. For more information about php tutorials, see. Original article: http://www.jbxue.com/php/25502.html
This article introduces two methods for sending mails in php: sending mails using the built-in mail () function in PHP and sending mails using the mail class that encapsulates the SMTP protocol, we recommend that you use the mail class encapsulated by smtp to send emails.

How does php send emails? There are many methods, but the most common method is to use the smtp protocol to send emails. let's take a look.
Topic recommendation: php mail code Daquan

I. Use the built-in mail () function in php

Sample code:

$ To = "test@163.com"; // recipient
$ Subject = "Test"; // topic
$ Message = "This is a test mail! "; // Body
Mail ($ to, $ subject, $ message );

Direct error:
Warning: mail () [function. mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php. ini or use ini_set () inD:/www/Zend/email. php on line 10

An smtp server is required locally and the code is changed:

Sample code:

$ To = "test@163.com"; // recipient
$ Subject = "Test"; // email subject
$ Message = "This is a test mail! "; // Body of the email
Ini_set ('smtp ', 'smtp .163.com'); // The sending SMTP server
Ini_set ('smtp _ port', 25); // sending smtp server port
Ini_set ('sendmail _ from', "admin@163.com"); // sender's mailbox
Mail ($ to, $ subject, $ message );

Continue error:
Warning: mail () [function. mail]: SMTP server response: 553 authentication is required, smtp2, DNGowKD7v5BTDo9NnplVBA --. 1171S2 1301220947 inD:/www/Zend/email. php on line 9

How do I write verification information? Where can I configure it?

To use the mail () function to send emails, you must have an email server that can send emails without SMTP Authentication. However, the current SMTP mail server basically needs to be verified, so if you want to use it to send emails, you have to build a local SMTP server that does not need to be verified.
Conclusion: to use the mail () function to send emails, you must have an SMTP server that does not need to be verified.

II. use the mail class that encapsulates the SMTP protocol
We recommend that you use the SMTP protocol to send emails.

We recommend that you use the Mail class in the PEAR extension. it has powerful functions: it supports plain text and HTML-format emails. you can set encoding for each field, and the correct configuration will not cause Chinese garbled characters; attachments are supported.

On the server, you can use the pear install Mail command for quick installation. if you do not have sufficient server permissions, you can directly download the PHP source code of the class to include it.

Note: The Mail class depends on Net/SMTP. php and Mail/mime. php, which must be downloaded and included in use.

For detailed installation methods, visit http://pear.php.net/package/mail.

For example, Mail is used to send emails.

Sample code:

// Pear Mail extension
Require_once ('mail. php ');
Require_once ('mail/mime. php ');
Require_once ('Net/SMTP. php ');

$ Smtpinfo = array ();
$ Smtpinfo ["host"] = "smtp.163.com"; // SMTP server
$ Smtpinfo ["port"] = "25"; // SMTP server port
$ Smtpinfo ["username"] = "username@163.com"; // sender's mailbox
$ Smtpinfo ["password"] = "password"; // The sender's email password.
$ Smtpinfo ["timeout"] = 10; // Network timeout, in seconds
$ Smtpinfo ["auth"] = true; // login verification
// $ Smtpinfo ["debug"] = true; // debug mode

// Recipient list
$ MailAddr = array ('author er @ 163.com ');

// Sender display information
$ From = "Name ";

// Recipient display information
$ To = implode (',', $ mailAddr );

// Mail title
$ Subject = "this is a test email ";

// Body of the email
$ Content = "anything to write ";

// Mail body type, format, and encoding
$ ContentType = "text/html; charsets = utf-8 ";

// Newline character Linux: \ n Windows: \ r \ n
$ Crlf = "\ n ";
$ Mime = new Mail_mime ($ crlf );
$ Mime-> setHTMLBody ($ content );

$ Param ['text _ charset'] = 'utf-8 ';
$ Param ['HTML _ charset'] = 'utf-8 ';
$ Param ['head _ charset'] = 'utf-8 ';
$ Body = $ mime-> get ($ param );

$ Headers = array ();
$ Headers ["From"] = $ from;
$ Headers ["To"] = $;
$ Headers ["Subject"] = $ subject;
$ Headers ["Content-Type"] = $ contentType;
$ Headers = $ mime-> headers ($ headers );

$ Smtp = & Mail: factory ("smtp", $ smtpinfo );


$ Mail = $ smtp-> send ($ mailAddr, $ headers, $ body );
$ Smtp-> disconnect ();

If (PEAR: isError ($ mail )){
// Failed to send
Echo 'email sending failed: '. $ mail-> getMessage (). "\ n ";
}
Else {
// Sent successfully
Echo "success! \ N ";
}

The above describes two methods for sending mails in php: use smtp to send mails in php, including some content, and hope to help friends who are interested in PHP tutorials.

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.