[Php] phpcurlsmtp email _ PHP Tutorial

Source: Internet
Author: User
[Php] phpcurlsmtp sends an email. The company's cloud platform has disabled fsockopen. if you want to use an smtp Internet mailbox to send emails, you can only try to use curl for google first, I found many questions, but the company's cloud platform turned off fsockopen. if you want to use an smtp Internet mailbox to send emails, you can only try using curl.
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.

$ Telnet email SMTP service address 25
Trying email service IP address...
Connected to mail SMTP service address.
Escape character is '^]'.
220 exchange email server address: Microsoft esmtp mail Service ready at Sat, 2 Jun 2012 15:02:12 + 0800
EHLO 127.0.0.1
250-exchange email server address Hello [email service IP address]
December 250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
AUTH LOGIN
334 VXNlcm5hbWU6
Username (base64_encode)
334 UGFzc3dvcmQ6
Password (base64_encode)
235 2.7.0 Authentication successful
Mail from: sending address
250 2.1.0 Sender OK
Rcpt to: inbox address
250 2.1.5 Recipient OK
DATA
354 Start mail input; end .
Content to be sent (there are many related specifications here)
.
250 2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000 @ exchange email server address> Queued mail for delivery
QUIT
221 2.0.0 Service closing transmission channel
Connection closed by foreign host.

Php test code:

1 2 header ("content-type: text/html; charset = utf-8 ");
3 $ smtp = array (
4 "url" => "email SMTP server address ",
5 "port" => "mailbox SMTP server port", // generally 25
6 "username" => "username ",
7 "password" => "password ",
8 "from" => "sending address ",
9 "to" => "recipient address ",
10 "subject" => "test the title ",
11 "body" => "test content"
12 );
13
14 $ CRLF = "\ r \ n ";
15 $ test = "";
16 $ curl = curl_init ();
17
18 curl_setopt ($ curl, CURLOPT_URL, $ smtp ['URL']);
19 curl_setopt ($ curl, CURLOPT_PORT, $ smtp ['port']);
20 curl_setopt ($ curl, CURLOPT_TIMEOUT, 10 );
21
22 function inlineCode ($ str ){
23 $ str = trim ($ str );
24 return $ str? '=? UTF-8? B? '. Base64_encode ($ str ).'? = ':'';
25}
26
27 function buildHeader ($ headers ){
28 $ ret = '';
29 foreach ($ headers as $ k => $ v ){
30 $ ret. = $ k. ':'. $ v. "\ n ";
31}
32 return $ ret;
33}
34
35 //
36 $ header = array (
37 'return-path' => '<'. $ smtp ['from']. '> ',
38 'date' => Date ('r '),
39 'from' => '<'. $ smtp ['from']. '> ',
40 'Mime-version' => '1. 0 ',
41 'subobject' => inlineCode ($ smtp ['subobject']),
42 'to' => $ smtp ['to'],
43 'content-type' => 'text/html; charset = UTF-8; format = flowed ',
44 'content-Transfer-Encoding '=> 'base64'
45 );
46 $ data = buildHeader ($ header). $ CRLF. chunk_split (base64_encode ($ smtp ['body']);
47
48
49 $ content = "EHLO". $ smtp ["url"]. $ CRLF; // get hello first
50 $ content. = "auth login ". $ CRLF. base64_encode ($ smtp ["username"]). $ CRLF. base64_encode ($ smtp ["password"]). $ CRLF; // verify login
51 $ content. = "mail from:". $ smtp ["from"]. $ CRLF; // sending address
52 $ content. = "rcpt to:". $ smtp ["to"]. $ CRLF; // recipient address
53 $ content. = "DATA". $ CRLF. $ data. $ CRLF. ".". $ CRLF; // send content
54 $ content. = "QUIT". $ CRLF; // exit
55
56 curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true); // curl receives the returned data
57 curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, $ content );
58 $ test = curl_exec ($ curl );
59 var_dump ($ test );
60 echo"
\ R \ n ";
61 var_dump ($ content );
62
63 // end
64 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.
Think in coding

From bluefrog

Refer can only try to use curl for a first google, found a lot of questions,...

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.