I first Googled it and found that many questions were asked but there were no relevant answers, and no related classes were found in phpclasses. So I started to try curl while reading the stmp protocol.
SMTP protocol
You can find multiple related examples on the Internet. You can experiment with telnet to connect to the mail server.
Copy codeThe Code is as follows:
$ Telnet email SMTP service address 25
Trying email service IP address...
Connected to mail SMTP service address.
Escape character is '^]'.
Exchange email server address: Microsoft esmtp mail Service ready at Sat, 2 Jun 2012 15:02:12 + 0800
EHLO 127.0.0.1
-Exchange email server address Hello [email service IP address]
-SIZE
-PIPELINING
-DSN
-ENHANCEDSTATUSCODES
X-ANONYMOUSTLS
-AUTH NTLM LOGIN
-X-EXPS GSSAPI NTLM
-8 BITMIME
-BINARYMIME
-CHUNKING
-XEXCH50
XRDST
AUTH LOGIN
VXNlcm5hbWU6
Username (base64_encode)
UGFzc3dvcmQ6
Password (base64_encode)
2.7.0 Authentication successful
Mail from: Sending address
2.1.0 Sender OK
Rcpt to: inbox address
2.1.5 Recipient OK
DATA
Start mail input; end with <CRLF>. <CRLF>
Content to be sent (there are many related specifications here)
.
2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000 @ exchange email server address> Queued mail for delivery
QUIT
2.0.0 Service closing transmission channel
Connection closed by foreign host.
Php test code:
Copy codeThe Code is as follows:
<? Php
Header ("content-type: text/html; charset = UTF-8 ");
$ Smtp = array (
"Url" => "email SMTP server address ",
"Port" => "Mailbox SMTP server port", // generally 25
"Username" => "User Name ",
"Password" => "password ",
"From" => "Sending address ",
"To" => "recipient address ",
"Subject" => "test the title ",
"Body" => "Test content"
);
$ CRLF = "\ r \ n ";
$ Test = "";
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ smtp ['url']);
Curl_setopt ($ curl, CURLOPT_PORT, $ smtp ['Port']);
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 10 );
Function inlineCode ($ str ){
$ Str = trim ($ str );
Return $ str? '=? UTF-8? B? '. Base64_encode ($ str ).'? = ':'';
}
Function buildHeader ($ headers ){
$ Ret = '';
Foreach ($ headers as $ k => $ v ){
$ Ret. = $ k. ':'. $ v. "\ n ";
}
Return $ ret;
}
//
$ Header = array (
'Return-path' => '<'. $ smtp ['from']. '> ',
'Date' => Date ('R '),
'From' => '<'. $ smtp ['from']. '> ',
'Mime-version' => '1. 0 ',
'Subobject' => inlineCode ($ smtp ['subobject']),
'To' => $ smtp ['to'],
'Content-type' => 'text/html; charset = UTF-8; format = flowed ',
'Content-Transfer-Encoding '=> 'base64'
);
$ Data = buildHeader ($ header). $ CRLF. chunk_split (base64_encode ($ smtp ['body']);
$ Content = "EHLO". $ smtp ["url"]. $ CRLF; // get hello first
$ Content. = "auth login ". $ CRLF. base64_encode ($ smtp ["username"]). $ CRLF. base64_encode ($ smtp ["password"]). $ CRLF; // verify Login
$ Content. = "mail from:". $ smtp ["from"]. $ CRLF; // sending Address
$ Content. = "rcpt to:". $ smtp ["to"]. $ CRLF; // Receiving address
$ Content. = "DATA". $ CRLF. $ data. $ CRLF. ".". $ CRLF; // send content
$ Content. = "QUIT". $ CRLF; // exit
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true); // curl receives the returned data
Curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, $ content );
$ Test = curl_exec ($ curl );
Var_dump ($ test );
Echo "<br/> \ r \ n ";
Var_dump ($ content );
// End
Curl_close ($ curl );
The above is only the php
The package test + modification took nearly six hours to make the product code compatible with fsockopen and curl.
Write an smtp class compatible with fsockopen and curl for simple mail sending.