Send mail in php:smtp or mail function mode

Source: Internet
Author: User
Tags in domain
PHP implementation of the mail-sending class, two ways:
SMTP mode and mail function mode.

Code:

<?php/*** Mail Send class */class sendmail{public $smtp _host;     Public $smtp _port = 25;     Public $SMTP _user;     Public $SMTP _password;     Public $from _name;     Public $SendFromMail;     Public $mail _to;     Public $subject;     Public $message;     Public $headers = ';     Public $ContentType = ' html ';     Public $charset = ' windows-1251 ';     Public $smtp _DEBUG = true;     Public $socket;     Public $error;        Public $SendMailVia = ' SMTP '; Public Function construct () {if ($this->sendfrommail = =) {$this->sendfrommail = $t             his->smtp_user; }} Public Function Send ($mail _to = ", $subject =", $message = ") {if ($mail _to!=") {$this-&G             t;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-to:". $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\r\n "; } else {$SEND. = "Reply-to:". $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-priority:3\r\n\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, "", "line") {return false;} Fputs ($socket, "HELO". $this->smtp_host. "             \ r \ n "); if (! $this->server_parse ($socket, "+", line)) {if ($this->smtp_debug) $this->error = ' <p>                Can not send helo!</p> ';                Fclose ($socket);             return false;             } fputs ($socket, "AUTH login\r\n"); if (! $this->server_parse ($socket, "334", line)) {if ($this->smtp_debug) $this->error = ' <p>can not find a answer to a request authorization.</p> ';                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 = ' <p>                Login authorization was wasn't accepted by server!</p> ';                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 = ' <p> No password is not accepted as a true server!                Authorization error!</p> ';                Fclose ($socket);             return false; } fputs ($socket, "MAIL from: <" $this->smtp_user. "             >\r\n "); if (! $this->server_parse ($sockET, "+", line)) {if ($this->smtp_debug) $this->error = ' <p>unable to send command MAIL from:                </p> ';                Fclose ($socket);             return false; } fputs ($socket, "RCPT to: <". $mail _to.             ">\r\n"); if (! $this->server_parse ($socket, "+", line)) {if ($this->smtp_debug) $this->error = ' <p>                Unable to send command RCPT to: </p> ';                Fclose ($socket);             return false;             } fputs ($socket, "data\r\n"); if (! $this->server_parse ($socket, "354", line)) {if ($this->smtp_debug) $this->error = ' <p>                Unable to send command data</p> ';                Fclose ($socket);             return false; } fputs ($socket, $SEND. "             \r\n.\r\n "); if (! $this->server_parse ($socket, "+", line)) {if ($this->smtp_debug) $this->error = ' <p>unable to send the message body.                The letter was Sent!</p> ';                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, $matches 2)) {unset ($matches [0]); $matches [1] = Trim (str_rePlace (' "', ' ', $matches 2[1]));                      $matches [2] = Trim ($matches 2[0]);                 $MAILINGARR [] = $matches;     }} return $MAILINGARR; } Private Function Server_parse ($socket, $response, $line = line) {while (substr ($server _response, 3, 1)! = ') {if (! ( $server _response = fgets ($socket, ())) {if ($this->smtp_debug) $this->error = "<p> $line Prob Lems Sending mail!                $response </p> ";           return false; }} if (! ( substr ($server _response, 0, 3) = = $response)) {if ($this->smtp_debug) $this->error = "<p> $line Pro Blems Sending mail!            $response </p> ";         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 >) {$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 ' starts or ends with. ';       $isValid = false;          } else if (Preg_match ('/\\.\\./', $local)) {$msg = ' local ' has a 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 have 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 Q          Uoted '; if (!preg_match ('/^ "(\\\\" |[          ^ "]) +" $/", Str_replace (" \\\\ "," ", $local))) {$isValid = false; }} if ($isValid &&! ( CHECKDNSRR ($domain, "MX") | | CHECKDNSRR ($domain, "A")) {$msg = ' domain <b> ' $domain. '          </b> not found in DNS ';       $isValid = false;     }} $this->error = $msg;   return $isValid; }}?>

Invocation Example:

<?php include "sendmail.class.php"; $Mail = new SendMail ();  Set congif $Mail->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 <recipientmail2@mydomain.com>, " Recipient Name "<recipient@mail.com>", ' My subject ', ' my message here. ') {echo ' Mail has been sent! ';} else {echo $Mail->error;}//Example 2 (Mail from me) $Mail->mail_to = ' mymail1@mydomain.com; Recipient2 name <recipientmail2@mydomain.com>, "Recipient name" <recipient@mail.com> "; $Mail->subject = ' My subject '; $Mail->message = ' My message here '; if ($Mail->send ()) {echo ' message sent! ';} else {echo $Mail->error;}//Example 3 (mail from another user:example user2@si te2.com) $Mail->mail_to = ' Recipient Name <recipientmail@domain.com> '; $Mail->subject = ' My subject '; $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;}?>

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.