Send email in PHP

Source: Internet
Author: User
Tags pear php class vars server port

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

After reading the manual, I began to write the code directly, as follows

<?php

$to = "[email protected]"; //Recipient

$subject = "Test"; //Theme

$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 ()

It seems that the local need to have an SMTP server, then use others to try it, and changed the code:

<?php

$to = "[email protected]"; 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 ', +); Outgoing SMTP server port

Ini_set (' sendmail_from ',"[email protected]"); 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

It seems that the need to verify information, how to write verification information? Where to configure it? Surfing the internet for a long while did not find out why, finally looked at others some technical articles to come to the conclusion (because of SMTP mail what is not very understanding, so do not know whether this conclusion is correct): Using the Mail () function to send mail must have a mail server without SMTP authentication can be sent. 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. This is more trouble, I do not want to the whole, interested students can try to build one, with Windows to bring IIS can, or download other SMTP server software from the Internet, I do not say much.

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 unrealistic, so use the SMTP protocol yourself to send mail.

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.

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

<?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"] = "[email protected]"; //Sender mailbox

$smtpinfo ["password"] = "password"; Sender Mailbox Password

$smtpinfo ["timeout"] = ten; Network timeout time, seconds

$smtpinfo ["auth"] = true; Login Verification

$smtpinfo ["Debug"] = true;//Debug mode

Recipient list

$MAILADDR = Array (' [email protected] ');

Sender Display Information

$from = "Name <[email protected]>";

Recipient Display Information

$to = Implode (', ',$mailAddr);

Message header

$subject = "This is a test e-mail";

Message body

$content = "

Message body type, format, and encoding

$contentType = "text/html;  Charset=utf-8 ";

NewLine symbol 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"] = $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)) {

Send failed

Echo ' Email sending failed: '. $mail->getmessage ().  "\ n";

}

else{

Sent successfully

echo "success!\n";

}

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.

Send email in PHP

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.