_php tips for sending mail via Curl SMTP in PHP

Source: Internet
Author: User
Tags auth curl server port
First Google a bit, found a lot of questions related to the question but no relevant solutions, in the phpclasses also did not find the relevant classes so they look at the stmp of the relevant agreement to begin to try Curl
SMTP protocol
This can be found on the internet a number of relevant examples, you can experiment with Telnet to connect mail server
Copy Code code 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 <CRLF>.<CRLF>
The content to be sent (there are a lot of relevant 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 Code code as follows:

<?php
Header ("Content-type:text/html;charset=utf-8");
$SMTP = Array (
"url" => "Mailbox SMTP server Address",
"Port" => "Mailbox SMTP Server Port",//General 25
"username" => "username",
"Password" => "password",
"From" => "Send Address",
"To" => "receiving address",
"Subject" => "test the title",
"Body" => "test the 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; Let's have a hello.
$content. = "AUTH LOGIN". $CRLF. Base64_encode ($smtp ["username"]). $CRLF. Base64_encode ($smtp ["Password"]). $CRLF; Verify Login
$content. = "MAIL from:". $smtp [' from ']. $CRLF; Send Address
$content. = "RCPT to:" $smtp ["to"]. $CRLF; Inbox Address
$content. = "DATA". $CRLF. $data. $CRLF. "." $CRLF; Send content
$content. = "QUIT". $CRLF; Exit
curl_setopt ($curl, Curlopt_returntransfer, true); Curl Receive 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 just a test of PHP
Package test + modification took nearly 6 hours to make the product code compatible with Fsockopen and curl
Later, write an SMTP class that is compatible with Fsockopen and curl simple send mail

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.