Use SMTP to send emails

Source: Internet
Author: User

Introduction: This is a detailed page for sending emails using SMTP. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339600 'rolling = 'no'>

$ Params = array ('smtpserver' => 'mail .xxx.com ', 'smtpauth' => '1', 'smtpuser' => 'System @ xxx.com ', 'smtppassword' => 'xxx', 'systemmailer' => 'cor Inc. ', 'systemmailaddress' => 'System @ xxx.com',); define ("mail_smtp_server", $ Params ['smtpserver']); define ("mail_auth ", $ Params ['smtpau']); define ("mail_username", $ Params ['smtpuser']); define ("mail_password", $ Params ['smtppassword']); define ('System _ Mailer ', $ Params ['systemmailer']); define ("support_email", $ Params ['systemmailaddress']); /*** send mail via SMTP ** @ Param array $ to * example array ("will" => "will@126.com") the array key value is the recipient's name, the array value is the recipient email address, which can contain multiple recipients at the same time; * @ Param string $ from_name sender name * @ Param string $ from sender email address * @ Param string $ subject Email Subject * @ Param string $ message email content note: HTML * @ Param string $ charset email encoding is supported. By default, it is specified as GBK * @ return int *. The returned value 0 indicates that the email is successfully sent. If the value is not 0, the email fails to be sent. */Function Sendmail ($ to, $ from_name, $ from, $ subject, $ message, $ charset = "GBK") {// send an email if (! $ From_name) $ from_name = system_mailer; If (! $ From) $ from = support_email; If (system_mailer & function_exists ('mail') // use the Sendmail component to send an email {$ message = str_replace ("\ r ", '', $ message); If (! Is_array ($ to) Return-7; foreach ($ to as $ key => $ email) {if ($ email) {@ mail ($ email, $ subject, $ message, "From: $ from") ;}} else // use SMTP to send an email {// return smtpsendmail ($ TMP, $ from_name, $ from, $ subject, $ message, $ charset); // mass mailing if (is_array ($ to) {foreach ($ to as $ key => $ value) {$ TMP [$ key] = $ value; If (count ($ TMP) = 10) {$ result [] = smtpsendmail ($ TMP, $ from_name, $ from, $ subject, $ message, $ charset); $ TMP = "" ;}} if ($ TMP) {Return smtpsendmail ($ TMP, $ from_name, $ from, $ subject, $ message, $ charset);} else {return array_pop ($ result );}} else {return-7 ;}} return 0;} function smtpsendmail ($ to, $ from_name, $ from, $ subject, $ message, $ charset = "GBK ") {$ SMTP = explode (":", mail_smtp_server); $ check = mail_auth; $ smtp_host = $ SMTP [0]; $ smtp_port = $ SMTP [1]? $ SMTP [1]: 25; $ mail_debug = $ globals ['queue _ debug']; if ($ check) {$ username = mail_username; $ Password = mail_password ;} $ s_from = support_email; $ fp = @ fsockopen ($ smtp_host, $ smtp_port, $ errno, $ errstr, 10); If (! $ FP) Return-1; if (function_exists ("stream_set_blocking") {stream_set_blocking ($ FP, true);} else {set_socket_blocking ($ FP, true );} stream_set_timeout ($ FP, 10); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastmessage. "\ r \ n";} If (substr ($ lastmessage, 0, 3 )! = 220) Return-1; $ yourname = "yourname"; if ($ check = "1") $ lastact = "HELO ". $ yourname. "\ r \ n"; else $ lastact = "HELO ". $ yourname. "\ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage, 0, 3 )! = 220) Return-2; if ($ check = "1") {$ lastact = "auth login ". "\ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage, 0, 3 )! = 334) Return-3; $ lastact = base64_encode ($ username ). "\ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage, 0, 3 )! = 334) Return-4; $ lastact = base64_encode ($ password ). "\ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage, 0, 3 )! = "235") Return-5 ;}$ lastact = "mail from: <$ s_from> \ r \ n"; fputs ($ FP, $ lastact ); $ lastmessage = fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage )! = 250) Return-6; $ I = 0; If (is_array ($ to) {foreach ($ to as $ key => $ email) {if ($ email) {$ lastact = "rcpt to: <$ email> \ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = fgets ($ FP, 512 ); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage, 250) =) {// return-7; $ I ++ ;}$ tomails [] = "$ key <$ email>" ;}} if ($ I <1) Return-7 ;} else {return-7 ;}$ lastact = "data \ r \ n"; fputs ($ FP, $ lastact); $ lastmessage = Fgets ($ FP, 512); // if ($ mail_debug) {echo $ lastact; echo $ lastmessage;} If (substr ($ lastmessage )! = 354) Return-8; $ head. = "mime-version: 1.0 \ r \ n"; $ head. = "Content-Type: text/html; charset = $ charset \ r \ n"; $ head. = "Subject: $ subject \ r \ n"; $ message = $ head. "\ r \ n ". $ message; $ head = "from :\"". $ from_name. "\" <$ from> \ r \ n "; $ message = $ head. $ message; // $ head = ":\"". $ to_name. "\" <$ to> \ r \ n "; $ head =" :". implode (",", $ tomails ). "\ r \ n"; $ message = $ head. $ message; $ message. = "\ r \ n. \ r \ n "; fputs ($ FP, $ message); // if ($ mail_debug) {$ lastmessage = fgets ($ FP, 512); echo $ head; echo $ lastmessage;} $ lastact = "Quit \ r \ n"; fputs ($ FP, $ lastace); fclose ($ FP); Return 0 ;}

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/339600.html pageno: 7.

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.