PHP Mail sent in two ways, mail sent two ways _php tutorial

Source: Internet
Author: User

PHP Mail sent in two ways, mail sent two ways


The main content of this article is to use PHP to send e-mail, summed up in the following two ways:

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

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:

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.

Let me give an example of how to send a message in the mail class, the other SMTP message classes on the web use a similar piece, 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
 
  
   ";  The recipient displays the information $to = Implode (', ', $mailAddr);  Message header $subject = "This is a test message"; 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 "; } else{//Send successful 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.

The above is the use of PHP to send e-mail two ways, have their own thinking process, I hope that everyone's learning has helped.

http://www.bkjia.com/PHPjc/1065583.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065583.html techarticle PHP Mail sent two ways, the mail sent two ways the main content of this article is to use PHP to send e-mail, summed up in the following two ways: first, the use of PHP built-in ...

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