PHP sends mail via mail () or socket

Source: Internet
Author: User

How PHP sends mail via mail () or socket 1.PHP

PHP sends the message "very simple" because he provides the mail () function to send directly, but this also follows

Register Globals became the second biggest killer for beginners.

(1) Send mail via mail () function

(2) via socket communication, use SMTP transport

Use of the 2.mail () function

The mail () function allows you to send e-mail directly from a script.

Returns True if the post of the message was successfully received, otherwise false.

Mail (to,subject,message,headers,parameters)

to required. Specify the recipient of the message.

Subject required. Specify the subject of the message. The parameter cannot contain any line-wrapping characters.

Message required. Specifies the message to be sent.

Headers is optional. Specify additional headers, such as from, Cc, and BCC.

Parameters is optional. Specifies additional parameters for the SendMail program.

Configuration of the PHP.ini

Mail configuration

SMTP = localhost
; Http://php.net/smtp-port
Smtp_port = 25

if(@Mail($to,$subject,$message,$headers)) {    Echo"Support Mail Send";} Else {    $SMTP=Newsmtp$smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//a true in this case is that authentication is used, otherwise it is not used.    $SMTP->debug =FALSE;//whether to display debug information sent    $SMTP->sendmail ($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);}

3.socket () mode sending principle

Use the Fsockopen function to open an Internet connection

Fsockopen (string hostname, int port, int [errno], string [errstr], int [timeout]);

This is because the SMTP protocol is used, so the port number is 25. After a successful connection is opened, a

Socket handle, you can use it as if you were using a file handle. The available operations are fputs (),

Fgets (), feof (), fclose ()

220 Service Ready (This information is returned when the socket connection is successful)

221 being processed

250 Request mail action is correct,

354 Start sending data, end with.

500 syntax error, command not recognized

550 command cannot be executed, invalid mailbox

552 Interrupt Handling: User exceeds file space

functionsmtp$relay _host= "",$smtp _port= 25,$auth=false,$user,$pass){$this->debug =FALSE;$this->smtp_port =$smtp _port;$this->relay_host =$relay _host;$this->time_out = 30;//is used in Fsockopen ()#$this->auth =$auth;//Auth$this->user =$user;$this->pass =$pass;#$this->host_name = "localhost";//is used in HELO command$this->log_file = "";$this->sock =FALSE;}functionSendMail$to,$from,$subject= "",$body= "",$mailtype,$CC= "",$BCC= "",$additional _headers= ""){$mail _from=$this->get_address ($this->strip_comment ($from));$body=ereg_replace("(^| (\ r \ n)) (\ \) "," \\1.\\3 ",$body);$header. = "mime-version:1.0\r\n";if($mailtype= = "HTML"){$header. = "content-type:text/html\r\n";}$header. = "To:".$to." \ r \ n ";if($CC!= "") {$header. = "Cc:".$CC." \ r \ n ";}$header. = "From:$from< ".$from." >\r\n ";$header. = "Subject:".$subject." \ r \ n ";$header.=$additional _headers;$header. = "Date:".Date("R"). " \ r \ n ";$header. = "X-mailer:by Redhat (php/").phpversion().") \ r \ n ";List($msec,$sec) =Explode(" ",Microtime());$header. = "Message-id: <".Date("Ymdhis",$sec).".". ($msec*1000000). ".".$mail _from." >\r\n ";$TO=Explode(",",$this->strip_comment ($to));if($CC!= "") {$TO=Array_merge($TO,Explode(",",$this->strip_comment ($CC)));}if($BCC!= "") {$TO=Array_merge($TO,Explode(",",$this->strip_comment ($BCC)));}$sent=TRUE;foreach($TO  as $rcpt _to) {$rcpt _to=$this->get_address ($rcpt _to);if(!$this->smtp_sockopen ($rcpt _to)) {$this->log_write ("Error:cannot send email to".$rcpt _to." \ n ");$sent=FALSE;Continue;}if($this->smtp_send ($this->host_name,$mail _from,$rcpt _to,$header,$body)) {$this->log_write ("e-mail has been sent to <".$rcpt _to." >\n ");} Else {$this->log_write ("Error:cannot Send email to <".$rcpt _to." >\n ");$sent=FALSE;}fclose($this-sock);$this->log_write ("Disconnected from remote host\n");}Echo"<br>";Echo $header;return $sent;}

Implementation code:

<?PHPrequire_once("email.class.php");$to= ' [email protected] ';$subject= ' The subject ';$message= ' Hello ';$headers= ' From: [email protected] '.    "\ r \ n". ' Reply-to: [email protected] '.    "\ r \ n". ' x-mailer:php/'.phpversion();$smtpserver= "smtp.163.com";//SMTP Server$smtpserverport= 25;//SMTP Server Port$smtpusermail= "[email protected]";//user mailboxes for the SMTP server$smtpemailto= "[email protected]";//Send to who$smtpuser= "Jasonchen_love";//user account for the SMTP server$smtppass= "123456";//user password for SMTP server$mailsubject= "php test mail system";//Message Subject$mailbody= "//Message Content$mailtype= "HTML";//message Format (html/txt), TXT for text mailif(@Mail($to,$subject,$message,$headers)) {    Echo"Support Mail Send";} Else {    $SMTP=Newsmtp$smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//a true in this case is that authentication is used, otherwise it is not used.    $SMTP->debug =FALSE;//whether to display debug information sent    $SMTP->sendmail ($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);}?>

PHP sends mail via mail () or socket

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.