Two ways to send mail using PHP

Source: Internet
Author: User
Tags pear php class php source code how to send mail server port zend

Today I studied the use of PHP to send e-mail, summed up, there are two ways:

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

After looking at the manual, I started to write the code directly, as follows

<?php
$to = "test@163.com";//Recipient
$subject = "Test";//subject
$message = "This is a test mail!";//Body
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 use Ini_set () ind:/www/zend/email/email.php on

It seems that the local need to have an SMTP server, then use others to try it, and then changed the 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 an 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

It seems to be necessary to verify the information, how to write validation information. Where to configure it. Internet search for a long time also did not find out why, finally, after reading some of the technical articles to conclude (because the SMTP mail is not very knowledgeable about what, so I do not know whether this conclusion is correct): the Mail () function to send a message must have an SMTP authentication can be a mail server. But now the SMTP mail server is basically the need to verify, so you want to use it to send a message, you can only build a local SMTP server do not need to authenticate. This is more trouble, I do not want to the whole, interested students can try to build their own, with Windows from the IIS can, or download other SMTP server software from the Internet, I do not say more.

Conclusion: using the mail () function to send a message, you must have an SMTP server that does not need to be authenticated.

This way the configuration work will be a little more, but the use of the time is more convenient, a few lines of code can be.

II. message classes using the encapsulated SMTP protocol

This method is more common, especially for the vast majority of their own no server, from the Internet to buy virtual host students,

The first approach is unrealistic, so use the SMTP protocol yourself to send mail.

However, to complete this work, you need to have a certain understanding of the SMTP protocol, like hands-on students can write a do-it-yourself, like copycat students can download from the Internet, there are many.

However, I recommend using the Mail class in the pear extension, powerful: can support plain text, HTML-formatted messages, the fields can be set to encode, the correct configuration does not appear garbled in Chinese, you can support attachments and so on.

In the server can use the Pear install Mail command to install quickly, not enough server permissions students can also directly download the class of PHP source code included in it.

Note: The Mail class relies on net/smtp.php and mail/mime.php, a piece to download, and a piece to include when used.

Detailed installation method can be viewed on the official website, Http://pear.php.net/package/Mail.


Let me give an example of how to send mail in the Mail class, the other SMTP message classes on the Internet is similar to the use of a method, 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 authentication//$smtpinfo ["Debug"] = true;//Debug mode//Recipient list $MAILADDR = Array (' receiver@163.com ');

The sender displays the information $from = "Name <username@163.com>";

The recipient displays information $to = Implode (', ', $mailAddr);

Message header $subject = "This is a test email";

Message body $content = " 

If the SMTP classes you find on the web are highly encapsulated, it is simpler to use than the above, but the methods are similar.

Conclusion: this way to send a message without any software, only need to include a PHP class, and then write a few more lines of configuration code, you can. And there are many examples of code on the Web, many times just copy over and then modify the individual several parameters can be used, so it will be very convenient, recommended this method.

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.