PHP uses socket to send emails without the need for SMTP service

Source: Internet
Author: User
Tags mail code
Sending mail via socket means sending the above commands to the mail server through the client, so as to realize the SMTP protocol to send mail. PHP uses the socket method to send the mail code as follows :/***

Sending mail via socket means sending the above commands to the mail server through the client, so as to realize the SMTP protocol to send mail. PHP uses the socket method to send the mail code as follows:

/*** SMTP configuration example ** $ config ['smtp _ host'] = smtp server address; * $ config ['smtp _ user'] = SMTP user account; * $ config ['smtp _ pass'] = smtp password; * $ config ['smtp _ port'] = SMTP port; ** @ param string $ from sender's email address * @ param string $ to multiple recipient's email addresses, separated by commas * @ param string $ subject email Title * @ param string $ content email content * @ param array $ config SMTP configuration * @ return boolean */function send_mail ($ from, $ to, $ subject, $ content, $ config) {I F (! $ Fp = fsockopen ($ config ['smtp _ host'], $ config ['smtp _ port'], $ errno, $ errstr, 30) {return FALSE ;} stream_set_blocking ($ fp, TRUE); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = '000000') {return FALSE;} fputs ($ fp, "EHLO discuz \ r \ n"); $ lastmessage = fgets ($ fp, 220 ); if (substr ($ lastmessage, 0, 3 )! = 220 & substr ($ lastmessage, 0, 3 )! = 250) {return FALSE;} while (1) {if (substr ($ lastmessage, 3, 1 )! = '-' | Empty ($ lastmessage) {break;} $ lastmessage = fgets ($ fp, 512);} fputs ($ fp, "auth login \ r \ n"); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 334) {return FALSE;} fputs ($ fp, base64_encode ($ config ['smtp _ user']). & quot; \ r \ n & quot;); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 334) {return false;} fputs ($ fp, base64_encode ($ config ['smtp _ pass']). & quot; \ r \ n & quot;); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 235) {return FALSE;} fputs ($ fp, "mail from: <". preg_replace ("/. * \ <(. + ?) \>. */"," \ 1 ", $ from ). "> \ r \ n"); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 250) {fputs ($ fp, "mail from: <". preg_replace ("/. * \ <(. + ?) \>. */"," \ 1 ", $ from ). "> \ r \ n"); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 250) {return FALSE ;}}$ email_tos = array (); foreach (explode (',', $ to) as $ touser) {$ touser = trim ($ touser); if ($ touser) {fputs ($ fp, "rcpt to: <". preg_replace ("/. * \ <(. + ?) \>. */"," \ 1 ", $ touser ). "> \ r \ n"); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 250) {fputs ($ fp, "rcpt to: <". preg_replace ("/. * \ <(. + ?) \>. */"," \ 1 ", $ touser ). "> \ r \ n"); $ lastmessage = fgets ($ fp, 512); return FALSE ;}} fputs ($ fp, "DATA \ r \ n "); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 354) {return FALSE;} $ maildelimiter = "\ r \ n"; $ subject = '=? '. $ Config ['charset'].'? B? '. Base64_encode (str_replace ("\ r", '', $ subject )).'? = '; $ Content = chunk_split (base64_encode (str_replace ("\ r \ n. "," \ r \ n .. ", str_replace (" \ n "," \ r \ n ", str_replace (" \ r "," \ n ", str_replace (" \ r \ n ", "\ n", str_replace ("\ n \ r", "\ r", $ content); $ from = preg_match ('/^ (. + ?) \ <(. + ?) \> $/', $ From, $ matched )? '=? '. $ Config ['charset'].'? B? '. Base64_encode ($ matched [1]). "? = <$ Matched [2]> ": '=? '. $ Config ['charset'].'? B? '. Base64_encode (str_replace ("\ r", '', $ from )).'? = '; $ Headers = "From: {$ from} {$ maildelimiter} X-Priority: 3 {$ maildelimiter} X-Mailer: Discuz! 2.0 {$ maildelimiter} MIME-Version: 1.0 {$ maildelimiter} Content-type: text/{$ config ['mailtype']}; charset = {$ config ['charset']} {$ maildelimiter} Content-Transfer-Encoding: base64 {$ maildelimiter} "; $ headers. = 'message-ID: <'. date ('ymmdhs '). '. '. substr (md5 ($ content. microtime (), 0, 6 ). rand (100000,999 999 ). '@'. $ _ SERVER ['http _ host']. ">{$ maildelimiter}"; fputs ($ fp, "Date :". date ('r '). "\ r \ n" ); Fputs ($ fp, ":". $. "\ r \ n"); fputs ($ fp, "Subject :". $ subject. "\ r \ n"); fputs ($ fp, $ headers. "\ r \ n"); fputs ($ fp, "\ r \ n"); fputs ($ fp, $ content. "\ r \ n. \ r \ n "); $ lastmessage = fgets ($ fp, 512); if (substr ($ lastmessage, 0, 3 )! = 250) {return FALSE;} fputs ($ fp, "QUIT \ r \ n"); return TRUE ;}

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.