php Send mail Two ways PHP uses SMTP to send mail

Source: Internet
Author: User
Tags mail code pear php send mail zend
This article introduces two methods of sending mail in PHP, using the PHP built-in mail () function to send mail, sending mail using the message class encapsulating the SMTP protocol, and recommending mail delivery using the SMTP protocol encapsulated message class.

How does php send mail? There are many ways to do this, but using the SMTP protocol to send messages is the most common way to learn.
Featured :php e-mail Code Daquan

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

Copy Code code example:

$to = ""; Recipient
$subject = "Test"; Theme
$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, verify your "SMTP" and "smtp_port "Setting in php.ini or with Ini_set () ind:/www/zend/email/email.php on line 10

The SMTP server is required locally and the code is modified to:

Copy Code code example:

$to = "";//Recipient
$subject = "Test";//Message subject
$message = "This is a test mail!"; /Message body
Ini_set (' smtp ', ' ');//Outgoing SMTP server
Ini_set (' Smtp_port ', 25);//Outgoing SMTP server port
Ini_set (' Sendmail_from ', "");//Sender 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/email.php on line 9

Need to verify information, how to write verification information? Where to configure it?

To send mail using the mail () function, you must have a mail server that can be sent to you 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.
Conclusion: When sending mail using the mail () function, you must have an SMTP server that does not require authentication.

II. using message classes that encapsulate the SMTP protocol
We recommend that you use the SMTP protocol to send messages.

It is recommended to use the mail class in the pear extension, 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://

example, the Mail class sends a message method.

Copy Code code example:

Pear Mail Extension
Require_once (' mail.php ');
Require_once (' mail/mime.php ');
Require_once (' net/smtp.php ');

$smtpinfo = Array ();
$smtpinfo ["host"] = "";//SMTP server
$smtpinfo ["port"] = "25"; SMTP Server port
$smtpinfo ["username"] = ""; Sender Mailbox
$smtpinfo ["password"] = "password";//sender's mailbox password
$smtpinfo ["Timeout"] = 10;//Network time-out, seconds
$smtpinfo ["auth"] = true;//Login Verification
$smtpinfo ["Debug"] = true;//Debug mode

Recipient list
$mailAddr = Array (' ');

Sender Display Information
$from = "Name ";

Recipient Display Information
$to = Implode (', ', $mailAddr);

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

Message body
$content = "

Write something, whatever.


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 ";
Sent successfully
echo "success!\n";

The above on the introduction of PHP two ways to send mail PHP using SMTP to send mail, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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: 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.