Send 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


$ Telnet Mailbox SMTP Service address 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 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-bi Narymime-chunking-xexch50 xrdst AUTH LOGIN vxnlcm5hbwu6 user name (base64_encode) ugfzc3dvcmq6 password (base64_encode) 2.7.0 Authent Ication 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> what to send (there are many related specifications here).  2.6.0 <[email protected] Mailbox server address > Queued mail for delivery QUIT 2.0.0 Service closing transmission channel Connection Closed by foreign host.

PHP code


<?php//author http://www.lai18.comheader ("Content-type:text/html;charset=utf-8"); $SMTP = Array ("url" = "e-mail SMTP server address", "port" = "Mailbox SMTP server Port",//General "username" + "username", "password" = " Code "," from "and" from "and" Send Address "," to "and" "to" and "to" and "to", "subject" and "Test title", "Body" and "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 ' and ' = ' $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 the login $content. = "MAIL from:". $smtp ["from"]. $CRLF; The sender address $content. = "RCPT to:". $smtp ["to"]. $CRLF; The pickup address $content. = "DATA". $CRLF. $data. $CRLF. ".". $CRLF; Send content $content. = "QUIT". $CRLF; Exit curl_setopt ($curl, Curlopt_returntransfer, true); Curl receives return 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);

Curl Technical Knowledge Tutorial series of technical course finishing
http://blog.csdn.net/hello_katty/article/details/45557423

Send mail via Curl SMTP in PHP

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.