Home > Backend Development > PHP Tutorial > Example of sending email through curl smtp in php

Example of sending email through curl smtp in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:05:18
Original
1016 people have browsed it
  1. /**
  2. Function: Send email
  3. url: http://bbs.it-home.org
  4. */
  5. header("content-type:text/html;charset=utf-8");
  6. $smtp = array(
  7. "url" => ; "Email SMTP server address",
  8. "port" => "Email SMTP server port", // Generally 25
  9. "username" => "Username",
  10. "password" => "Password",
  11. "from" => "Sending address",
  12. "to" => "Receiving address",
  13. "subject" => "Test the title",
  14. "body" => "Test the content "
  15. );
  16. $CRLF = "rn";
  17. $test = "";
  18. $curl = curl_init();
  19. curl_setopt($curl, CURLOPT_URL, $smtp['url']);
  20. curl_setopt($curl, CURLOPT_PORT, $smtp['port']);
  21. curl_setopt($curl, CURLOPT_TIMEOUT,10);
  22. function inlineCode($str){
  23. $str = trim($str);
  24. return $str?'=?UTF- 8?B?'.base64_encode($str).'?= ':'';
  25. }
  26. function buildHeader($headers){
  27. $ret = '';
  28. foreach($headers as $k=>$v ){
  29. $ret.=$k.': '.$v."n";
  30. }
  31. return $ret;
  32. }
  33. //
  34. $header = array(
  35. 'Return-path'=>'< ;'.$smtp['from'].'>',
  36. 'Date'=>date('r'),
  37. 'From'=> '<'.$smtp['from']. '>',
  38. 'MIME-Version'=>'1.0',
  39. 'Subject'=>inlineCode($smtp['subject']),
  40. 'To'=>$smtp['to'] ,
  41. 'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
  42. 'Content-Transfer-Encoding'=>'base64'
  43. );
  44. $data = buildHeader($ header).$CRLF.chunk_split(base64_encode($smtp['body']));
  45. $content = "EHLO ".$smtp["url"].$CRLF; // Hello first
  46. $content .= "AUTH LOGIN".$CRLF.base64_encode($smtp["username"]).$CRLF.base64_encode($smtp["password"]).$CRLF; // Verify login
  47. $content .= "MAIL FROM:" .$smtp["from"].$CRLF; // Sending address
  48. $content .= "RCPT TO:".$smtp["to"].$CRLF; // Receiving address
  49. $content .= " DATA".$CRLF.$data.$CRLF.".".$CRLF; // Send content
  50. $content .= "QUIT".$CRLF; // Exit
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); / / curl receives return data
  52. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $content);
  53. $test = curl_exec($curl);
  54. var_dump($test);
  55. echo "
    rn";
  56. var_dump($ content);
  57. // End
  58. curl_close($curl);
  59. ?>
Copy code

It took nearly 6 hours to make the product code compatible with fsockopen and curl; I will study it when I have time. curl's smtp class for simply sending emails.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template