Send email with socket in PHP (c)

Source: Internet
Author: User
Tags add continue empty end ereg connect response code socket
Mail delivery processing

Here is the real secret, but look carefully. :)

--------------------------------------------------------------------------------
1 function Send ($to, $from, $subject, $message)
2 {
3
4//Connection server
5 $this->lastact= "Connect";
6
7 $this->show_debug ("Connect to SMTP Server:". $this->smtp, "out");
8 $this->FP = Fsockopen ($this->smtp, $this->port);
9 if ($this->FP)
10 {
11
Set_socket_blocking ($this->FP, true);
$this->lastmessage=fgets ($this->fp,512);
$this->show_debug ($this->lastmessage, "in");
15
if (! Ereg ("^220", $this->lastmessage))
17 {
return false;
19}
Else
21 {
$this->lastact= "HELO". $this->welcome. "\ n";
if (! $this->do_command ($this->lastact, "250"))
24 {
Fclose ($this->FP);
return false;
27}
28
$this->lastact= "MAIL from: $from". "\ n";
if (! $this->do_command ($this->lastact, "250"))
31 {
Fclose ($this->FP);
return false;
34}
35
$this->lastact= "RCPT to: $to". "\ n";
Panax Notoginseng if (! $this->do_command ($this->lastact, "250"))
38 {
Fclose ($this->FP);
return false;
41}
42
43//Send Text
$this->lastact= "data\n";
if (! $this->do_command ($this->lastact, "354"))
46 {
Fclose ($this->FP);
return false;
49}
50
51//Processing subject Head
$head = "Subject: $subject \ n";
if (!empty ($subject) &&!ereg ($head, $message))
54 {
$message = $head. $message;
56}
57
58//Processing from head
$head = "From: $from \ n";
if (!empty ($from) &&!ereg ($head, $message))
61 {
$message = $head. $message;
63}
64
65//Processing to head
$head = "to: $to \ n";
if (!empty ($to) &&!ereg ($head, $message))
68 {
$message = $head. $message;
70}
71
72//Plus end string
if (!ereg ("\n\.\n", $message))
$message. = "\n.\n";
$this->show_debug ($message, "out");
Fputs ($this->fp, $message);
77
$this->lastact= "quit\n";
(! $this->do_command ($this->lastact, "250"))
80 {
Bayi fclose ($this->FP);
The return false;
83}
84}
return true;
86}
Or else
88 {
$this->show_debug ("Connect failed!", "in");
return false;
91}
92}
--------------------------------------------------------------------------------
Some of the meaning is very clear I will not say.

This function is a total of four parameters, is the $to of the addressee, $from the sender, $subject table for the message subject and $message to represent the message body. Returns true if the processing succeeds, or false if it fails.

Line 8th, connect the mail server if the success response code should be 220.
Line 12th, set blocking mode, indicating that the information must be returned to continue. Read the manual in detail.
Line 16th, to determine whether the response code is 220, if it is, then continue processing, or error return.
第22-27 line, processing helo instruction, expect response code is 250.
第29-34 line, processing the mail from directive, expecting a response code of 250.
第36-41 line, processing rcpt to instruction, the expected response code is 250.
第44-49 line, processing the data instruction, the expected response code is 354.
第51-76 line, generate the message body, and send.
第52-56 line, if $subject is not empty, find out if there is a topic part in the message body, and if not, add the topic section.
第59-63 line, if the $from is not empty, find out if there is a sender part of the message body, if not, add the sender part.
第66-70 line, if the $to is not empty, find out if there is a recipient portion of the message body, and if not, add the recipient portion.
第73-74 the line to find out if the message body has an end line, if not, plus the end line of the message body (with "."). As a special line on a single line).
Line 76th, send the message body.
第78-83 line, perform quit connection to the server and expect a response code of 250.
Line 85th returns the processing Success flag (TRUE).
第81-91 row, processing of failed connections to the server.
The above for the entire Send_mail class implementation, should not be difficult.


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.