Send mail via curlsmtp in php

Source: Internet
Author: User
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 I didn't have any answers. I didn't find any related classes 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.
The 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 .
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:
The code is as follows:
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"
\ 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.

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.