Php mail sending class (smtp or mail function)

Source: Internet
Author: User
Tags in domain
Php mail sending class (smtp or mail function)
This article introduces a class for sending email. you can use the smtp and mail functions to send emails. For more information, see.

Php implements mail sending in two ways: smtp mode and mail function mode.

Code:

     SendFromMail = '') {$ this-> SendFromMail = $ this-> smtp_user;} public function Send ($ mail_to ='', $ subject = '', $ message = '') {if ($ mail_to! = '') {$ This-> mail_to = stripslashes ($ mail_to);} if ($ subject! = '') {$ This-> subject = stripslashes ($ subject);} if ($ message! = '') {$ This-> message = $ message;} $ meilsArr = array_filter ($ this-> GetMailAndNameArr (); if (trim ($ this-> mail_to) = '') {$ this-> error = 'Enter the recipient address';} if ($ meilsArr = array ()) {$ this-> error = 'Please enter a valid recipient address';} foreach ($ meilsArr as $ val) {$ validEmail = $ this-> validEmail ($ val [2]); if ($ validEmail) {if ($ this-> SendMailVia = 'smtp ') {return $ this-> SMTPsend ($ mail_to = $ Val [2], $ name_to = $ val [1]);} else {return $ this-> MAILsend ($ mail_to = $ val [2], $ name_to = $ val [1]) ;}}} public function MAILsend ($ mail_to, $ name_to) {if ($ this-> ContentType = "text ") {$ header = "Content-Type: text/plain; charset = ". $ this-> charset. "";} else {$ header = "Return-Path :". $ this-> smtp_user. "\ n ". "Reply-:". $ this-> SendFromMail. "\ n ". "From :". $ this-> from_name. "<". $ this-> SendFromMail. "> \ N ". "Subject :". $ this-> subject. "\ n ". "Content-Type: text/html; charset = ". $ this-> charset. "\ n";} if (mail ("$ name_to <$ mail_to>", $ this-> subject, $ this-> message, $ header) {return true ;} else {return false ;}} public function SMTPsend ($ mail_to, $ name_to) {$ SEND = "Date :". date ("D, d m y h: I: s "). "\ r \ n"; $ SEND. = 'Subject: =? '. $ This-> charset .'? B? '. Base64_encode ($ this-> subject). "=? = \ R \ n "; if ($ this-> headers! = '') {$ SEND. = $ this-> headers. "\ r \ n";} else {$ SEND. = "Reply-:". $ this-> SendFromMail. "\ r \ n"; $ SEND. = "MIME-Version: 1.0 \ r \ n"; $ SEND. = "Content-Type: text /". $ this-> ContentType. "; charset = \"". $ this-> charset. "\" \ r \ n "; $ SEND. = "Content-Transfer-Encoding: 8bit \ r \ n"; $ SEND. = "From :\"". $ this-> from_name. "\" <". $ this-> SendFromMail. "> \ r \ n"; $ SEND. = "To: $ name_to <$ mail_to> \ r \ n"; $ SEND. = "X-Pri Ority: 3 \ r \ n ";} $ SEND. = $ this-> message. "\ r \ n"; $ socket = fsockopen ($ this-> smtp_host, $ this-> smtp_port, $ errno, $ errstr, 30); if (! Socket) {if ($ this-> smtp_debug) $ this-> error = $ errno. "-". $ errstr; return false;} if (! $ This-> server_parse ($ socket, "220", _ LINE _) {return false;} fputs ($ socket, "HELO ". $ this-> smtp_host. "\ r \ n"); if (! $ This-> server_parse ($ socket, "250", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Can not send HELO!

'; Fclose ($ socket); return false;} fputs ($ socket, "auth login \ r \ n"); if (! $ This-> server_parse ($ socket, "334", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Can not find an answer to a request authorization.

'; Fclose ($ socket); return false;} fputs ($ socket, base64_encode ($ this-> smtp_user). "\ r \ n"); if (! $ This-> server_parse ($ socket, "334", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Login authorization was not accepted by server!

'; Fclose ($ socket); return false;} fputs ($ socket, base64_encode ($ this-> smtp_password). "\ r \ n"); if (! $ This-> server_parse ($ socket, "235", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

No password was not accepted as a true server! Authorization Error!

'; Fclose ($ socket); return false;} fputs ($ socket, "mail from: <". $ this-> smtp_user. "> \ r \ n"); if (! $ This-> server_parse ($ socket, "250", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Unable to send command mail from:

'; Fclose ($ socket); return false;} fputs ($ socket, "rcpt to: <". $ mail_to. "> \ r \ n"); if (! $ This-> server_parse ($ socket, "250", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Unable to send command rcpt:

'; Fclose ($ socket); return false;} fputs ($ socket, "DATA \ r \ n"); if (! $ This-> server_parse ($ socket, "354", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Unable to send command DATA

'; Fclose ($ socket); return false;} fputs ($ socket, $ SEND. "\ r \ n. \ r \ n"); if (! $ This-> server_parse ($ socket, "250", _ LINE _) {if ($ this-> smtp_debug) $ this-> error ='

Unable to send the message body. The letter was sent!

'; Fclose ($ socket); return false;} fputs ($ socket, "QUIT \ r \ n"); fclose ($ socket); return TRUE ;} private function GetMailAndNameArr () {$ mailingArr = array (); $ tos = preg_split ("/; |,/", $ this-> mail_to); $ pregcode = '/(. *?) <(. *?)> /I '; foreach ($ tos as $ to) {if (preg_match ('/(.*?) <(. *?)> /I ', $ to, $ matches) {unset ($ matches [0]); $ matches [1] = trim (str_replace ('"','', $ matches [1]); $ matches [2] = trim ($ matches [2]); $ mailingArr [] = $ matches ;} elseif (preg_match ('/\ B ([A-Z0-9. _ %-] +) @ ([A-Z0-9. -] + \. [A-Z] {2, 4}) \ B/I ', $ to, $ matches2) {unset ($ matches [0]); $ matches [1] = trim (str_replace ('"','', $ matches2 [1]); $ matches [2] = trim ($ matches2 [0]); $ mailingArr [] = $ matches ;}return $ mailingArr;} p Rivate function server_parse ($ socket, $ response, $ line = _ LINE _) {while (substr ($ server_response, 3, 1 )! = '') {If (! ($ Server_response = fgets ($ socket, 256) {if ($ this-> smtp_debug) $ this-> error ="

$ Line Problems sending mail! $ Response

"; Return false ;}} if (! (Substr ($ server_response, 0, 3) ==$ response) {if ($ this-> smtp_debug) $ this-> error ="

$ Line Problems sending mail! $ Response

"; Return false;} return true;} function validEmail ($ email) {$ isValid = true; $ atIndex = strrpos ($ email ,"@"); $ msg = ''; if (is_bool ($ atIndex )&&! $ AtIndex) {$ isValid = false;} else {$ domain = substr ($ email, $ atIndex + 1); $ local = substr ($ email, 0, $ atIndex ); $ localLen = strlen ($ local); $ domainLen = strlen ($ domain); if ($ localLen <1 | $ localLen> 64) {$ msg = 'local part length exceeded '; $ isValid = false;} else if ($ domainLen <1 | $ domainLen> 255) {$ msg = 'domain part length exceeded '; $ isValid = false;} else if ($ local [0] = '. '| | $ Local [$ localLen-1] = '. ') {$ msg = 'local part starts or ends. '; $ isValid = false;} else if (preg_match ('/\\. \\. /', $ local) {$ msg = 'local part has two consecutive dots'; $ isValid = false;} else if (! Preg_match ('/^ [A-Za-z0-9 \-\.] + $/', $ domain) {$ msg = 'character not valid in domain part'; $ isValid = false;} else if (preg_match ('/\\. \\. /', $ domain) {$ msg = 'domain part has two consecutive dots'; $ isValid = false;} else if (! Preg_match ('/^ (\\\\. | [A-Za-z0-9! # % & '_ =\\/ $ \' * +? ^ {} | ~. -]) + $/', Str_replace ("\\\\", "", $ local ))) {$ msg = 'character not valid in local part unless local part is quoted'; if (! Preg_match ('/^ "(\\\\" | [^ "]) +" $/', str_replace ("\\\\","", $ local) {$ isValid = false ;}} if ($ isValid &&! (Checkdnsrr ($ domain, "MX") | checkdnsrr ($ domain, "A") {$ msg = 'domain'. $ Domain .'Not found in DNS '; $ isValid = false; }}$ this-> error = $ msg; return $ isValid ;}}?>

Call example:

     SendMailVia = 'smtp '; // Send via smtp server or mail function $ Mail-> smtp_host = 'mail .myhost.com'; $ mail-> smtp_port = 25; $ Mail-> smtp_user = 'User @ myhost.com '; $ Mail-> smtp_password = 'mypassw'; // Example 1 (mail from me) if ($ Mail-> Send ('mymail1 @ mydomain.com; recipient2 name
     
      
, "Recipient name"
      
       
', 'My subobject', 'My message here.') {echo 'the email has been sent! ';} Else {echo $ Mail-> error;} // Example 2 (mail from me) $ Mail-> mail_to = 'mymail1 @ mydomain.com; recipient2 name
       
        
, "Recipient name"
        
         
'; $ Mail-> subject = 'My subobject'; $ Mail-> message = 'My message here'; if ($ Mail-> Send ()) {echo 'email sent! ';} Else {echo $ Mail-> error;} // example 3 (mail from another user: example user2@site2.com) $ Mail-> mail_to = 'recipient Name
         
          
'; $ Mail-> subject = 'My subobject'; $ Mail-> message = 'My message here'; $ Mail-> from_name = 'user2 name '; $ Mail-> SendFromMail = 'user2 @ site2.com '; if ($ Mail-> Send () {echo 'message Mail send! ';} Else {echo $ Mail-> error;}?>
         
        
       
      
     

Related Article

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.