_php tutorial for sending mail via Curl SMTP in PHP

Source: Internet
Author: User
Google first, found a lot of questions related to the question but there is no relevant answer, in the phpclasses also found no related classes and then he looked at the stmp of the relevant protocol side began to try Curl
SMTP protocol
This can be found on the Internet more relevant examples, you can experiment with the use of Telnet to connect to the mail server
Copy CodeThe code is as follows:
$ Telnet Mailbox SMTP Service address 25
Trying Mailbox Service IP Address ...
Connected to mailbox SMTP service address.
Escape character is ' ^] '.
Exchange Mailbox server address Microsoft ESMTP MAIL Service ready at Sat, 2 June 2012 15:02:12 +0800
EHLO 127.0.0.1
-exchange Mailbox server address Hello [mailbox service IP Address]
-size
-pipelining
-dsn
-enhancedstatuscodes
-x-anonymoustls
-auth NTLM LOGIN
-x-exps GSSAPI NTLM
-8bitmime
-binarymime
-chunking
-xexch50
Xrdst
AUTH LOGIN
Vxnlcm5hbwu6
User name (Base64_encode)
Ugfzc3dvcmq6
Password (base64_encode)
2.7.0 Authentication Successful
MAIL From: Outbox address
2.1.0 Sender OK
RCPT to: Inbox address
2.1.5 Recipient OK
DATA
Start mail input; End With .
What to send (there are a number of related specifications here)
.
2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000@exchange Mailbox 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:
Header ("Content-type:text/html;charset=utf-8");
$SMTP = Array (
"url" = "Mailbox SMTP server Address",
"Port" \ = "Mailbox SMTP server Ports",//General 25
"Username" = "User name",
"Password" = "Password",
"From" and "from" to "address",
"to" + = "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 ',
' Subject ' =>inlinecode ($smtp [' Subject ']),
' 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; First, hello.
$content. = "AUTH LOGIN". $CRLF. Base64_encode ($smtp ["username"]). $CRLF. Base64_encode ($smtp ["Password"]). $CRLF; Verify Login
$content. = "MAIL from:" $smtp ["from"]. $CRLF; Sender Address
$content. = "RCPT to:" $smtp ["to"]. $CRLF; Pickup 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 just a test of PHP
Package test + modification took nearly 6 hours to make the product code compatible with Fsockopen and curl
Have time to write an SMTP class that fsockopen and curl simply sends mail

http://www.bkjia.com/PHPjc/325390.html www.bkjia.com true http://www.bkjia.com/PHPjc/325390.html techarticle Google first, found a lot of questions related to the question but there is no relevant answer, in the phpclasses also found no related class and then look at the stmp of the relevant protocol side began to try the Curl SMTP protocol ...

  • Related Article

    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.