Two ways to send PHP mail

Source: Internet
Author: User
Tags pear php class
There are a lot of programmers in the use of PHP to send the mail will encounter a few problems, then encounter problems will have to solve the problem of ideas, this article is based on the idea of sending mail to the PHP, collated down an article, in the face of different problems given the different solutions, interested in the small partners can refer to.

First, use the PHP built-in mail () function

<?php $to = "test@163.com"; Recipient $subject = "Test"; Subject $message = "This is a test mail!"; Text Mail ($to, $subject, $message);

The result is a direct error, as follows:

Warning:mail () [Function.mail]: Failed to connect to mailserver at "localhost" port, verify your "SMTP" and "smtp_port "Setting in php.ini or with Ini_set () ind:/www/zend/email/email.php on line 10

Analysis reason: The local need to have an SMTP server, and changed the following code:

<?php $to = "test@163.com";//Recipient $subject = "Test";//message subject $message = "This is a test mail!"; /Message body ini_set (' smtp ', ' smtp.163.com ');//Outgoing SMTP server Ini_set (' Smtp_port ', 25);//Outgoing SMTP server Port ini_set (' Sendmail_from ', " Admin@163.com ");//Sender Mailbox Mail ($to, $subject, $message);

The result is still error:

Warning:mail () [Function.mail]: SMTP server response:553 authentication is REQUIRED,SMTP2, Dngowkd7v5btdo9nnplvba--. 1171s2 1301220947 ind:/www/zend/email/email.php on line 9

Analysis Reason: Need to verify information, how to write verification information? Where to configure it? With these questions, refer to some technical articles and conclude that using the mail () function to send a message requires a mail server that can be sent without SMTP authentication. But now the SMTP mail server is basically need to verify, so if you want to use it to send e-mail, you can only set up a local SMTP server do not need authentication. How to build: You can use IIS with Windows, or download additional SMTP server software from the Web.

Conclusion: When sending mail using the mail () function, you must have an SMTP server that does not require authentication. This will be a bit more configuration work, but the use of time is more convenient, a few lines of code can be.

II. using message classes that encapsulate the SMTP protocol

This method is more common, especially for the vast number of their own no server, from the Internet to buy virtual host students, the first method is not realistic, so still use the SMTP protocol to send mail it.

But to complete this work, you need to have a certain understanding of the SMTP protocol, like hands students can write their own, like take doctrine students can download from the Internet, there are many.

However, I recommend the use of the pear extension of the Mail class, powerful: can support plain text, HTML-formatted messages, each field can be set encoding, the correct configuration will not appear in Chinese garbled situation, can support attachments and so on.

In the server can use the Pear install Mail command quickly installed, do not have sufficient server permissions of the students can also download the class of PHP source directly included in it.

Note: The Mail class relies on net/smtp.php and mail/mime.php for a piece of download, which is included in the use.

Below I give an example of how to send a message in the mail class, the other SMTP message class on the Web using a similar block, you can refer to:

<?php//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 mailbox $smtpinfo ["password"] = "password";//Sender Mailbox Password $smtpinfo ["timeout"] = 10;//network timeout, seconds $smtpinfo ["auth"] = true;// Login verification//$smtpinfo ["Debug"] = true;//Debug mode//Recipient list $MAILADDR = Array (' receiver@163.com '); Sender Display Information $from = "Name <username@163.com>"; The recipient displays the information $to = Implode (', ', $mailAddr); Message header $subject = "This is a test message"; Message body $content = "

If the SMTP classes that are found on the web are highly encapsulated, it is easier to use them than the above, but the methods used are quite similar.

Conclusion: This way to send mail without any software, only need to include a PHP class, and then write a few lines of configuration code, you can. And there are many examples of code online, many times as long as the copy and then modify the individual parameters can be used, so it is very convenient, recommended to use this method.

No matter what kind of problems encountered, methods thousand species, the most important thing is to have a problem-solving mentality, I hope this article can bring inspiration to everyone.

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.