Use Socket to send emails in PHP (3)

Source: Internet
Author: User
Tags ereg
Use a Socket in PHP to send an email (3) <br/> The following is a real secret. you should check it carefully. :) <Br/> --------------- mail sending and processing

The following are the real secrets. you should check them carefully. :)

--------------------------------------------------------------------------------
1 function send ($ to, $ from, $ subject, $ message)
2 {
3
4 // connect to the 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
12 set_socket_blocking ($ this-> fp, true );
13 $ this-> lastmessage = fgets ($ this-> fp, 512 );
14 $ this-> show_debug ($ this-> lastmessage, "in ");
15
16 if (! Ereg ("^ 220", $ this-> lastmessage ))
17 {
18 return false;
19}
20 else
21 {
22 $ this-> lastact = "HELO". $ this-> welcome. "\ n ";
23 if (! $ This-> do_command ($ this-> lastact, "250 "))
24 {
25 fclose ($ this-> fp );
26 return false;
27}
28
29 $ this-> lastact = "mail from: $ from". "\ n ";
30 if (! $ This-> do_command ($ this-> lastact, "250 "))
31 {
32 fclose ($ this-> fp );
33 return false;
34}
35
36 $ this-> lastact = "rcpt to: $ to". "\ n ";
37 if (! $ This-> do_command ($ this-> lastact, "250 "))
38 {
39 fclose ($ this-> fp );
40 return false;
41}
42
43 // send the body
44 $ this-> lastact = "DATA \ n ";
45 if (! $ This-> do_command ($ this-> lastact, "354 "))
46 {
47 fclose ($ this-> fp );
48 return false;
49}
50
51 // process the Subject header
52 $ head = "Subject: $ subject \ n ";
53 if (! Empty ($ subject )&&! Ereg ($ head, $ message ))
54 {
55 $ message = $ head. $ message;
56}
57
58 // process the From header
59 $ head = "From: $ from \ n ";
60 if (! Empty ($ from )&&! Ereg ($ head, $ message ))
61 {
62 $ message = $ head. $ message;
63}
64
65 // process the To header
66 $ head = "To: $ to \ n ";
67 if (! Empty ($ )&&! Ereg ($ head, $ message ))
68 {
69 $ message = $ head. $ message;
70}
71
72 // add the end string
73 if (! Ereg ("\ n \. \ n", $ message ))
74 $ message. = "\ n. \ n ";
75 $ this-> show_debug ($ message, "out ");
76 fputs ($ this-> fp, $ message );
77
78 $ this-> lastact = "QUIT \ n ";
79 if (! $ This-> do_command ($ this-> lastact, "250 "))
80 {
81 fclose ($ this-> fp );
82 return false;
83}
84}
85 return true;
86}
87 else
88 {
89 $ this-> show_debug ("Connect failed! "," In ");
90 return false;
91}
92}
--------------------------------------------------------------------------------
I won't say anything clearly.

This function has four parameters: $ to indicates the recipient, $ from indicates the sender, and $ subject indicates the subject of the email and $ message indicates the body of the email. If the processing is successful, true is returned. if the processing fails, false is returned.

Line 2: connect to the email server. if the response code is successful, it must be 8th.
Row 3 sets the blocking mode, indicating that the information must be returned to continue. For more information, see the manual.
In row 3, check whether the response code is 16th. If yes, continue to process the code; otherwise, an error is returned.
Line 22-27 processes the HELO command and the expected response code is 250.
Line 29-34 processes the mail from instruction and the expected response code is 250.
Line 36-41 processes the rcpt to command and the expected response code is 250.
Line 44-49 processes DATA commands and the expected response code is 354.
Line 51-76 generates and sends the email body.
Line 52-56. if $ subject is not empty, check whether the subject is included in the email body. if not, add the subject.
Line 59-63. if $ from is not blank, check whether the Mail body contains the sender part. if not, add the sender part.
Line 66-70. if $ to is not empty, check whether the email body contains the recipient. if not, add the recipient.
Row 73-74: Check whether the Mail body has an ending line. if not, add the ending line of the mail body (use "." as a special line for a separate line ).
Row 3: send the email body.
Line 8-83: execute QUIT to check whether the connection is established with the server. the expected response code is 250.
Row 3: returns the success mark (true ).
Line 81-91: Handling of connection failures with the server.
The above is the implementation of the entire send_mail class, which should not be very 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.