php Send mail function

Source: Internet
Author: User
Tags php send mail

<?php/** * Record Data */function runlog ($mode = ' SMTP ', $b = ', $c = ', $d = ') {}/** * Send mail * @param $toemail recipient Email * @para M $subject Message subject * @param $message body */function sendmail ($toemail, $subject, $message) {$config = array (' mailsend ' = =) 2, ' maildelimiter ' = + 1, ' mailusername ' + 1, ' server ' = ' smtp.qq.com ', ' Port ', ' + ', ' mail_type ' + 1, ' auth ' + 1, ' from ' = ' [email protected] ', ' auth_username ' = ' [email protected] ', ' auth_password ' = ' Password ',); $charset = ' utf-8 '; $mail = $config; $from = $config [' from ']; $mail _type = $config [' Mail_type '];//mai L Send mode if ($mail _type==0) {$headers = ' mime-version:1.0 '. "\ r \ n"; $headers. = ' content-type:text/html; Charset= '. $charset. "\ r \ n"; $headers. = ' From: < ' $from. ' > '. "\ r \ n"; Mail ($toemail, $subject, $message, $headers); return true;} Message header delimiter $maildelimiter = $mail [' maildelimiter '] = = 1? "\ r \ n": ($mail [' maildelimiter '] = = 2? "\ r": "\ n");//The recipient address contains the user name $mailusername = isset ($mail [' MailusErname '])? $mail [' Mailusername ']: 1;//port $mail[' port ' = $mail [' Port ']? $mail [' Port ']: $mail [' mailsend '] = $mail [' MailSend ']? $mail [' MailSend ']: 1;//sender $email_from = $from = = "? ' =? '. $charset. '? B? '. '? = < ". $from." > ": (Preg_match ('/^ (. +?) \< (. +?) \>$/', $from, $mats)? ' =? '. $charset. '? B? '. Base64_encode ($mats [1]). "? = < $mats [2]> ": $from); $email _to = Preg_match ('/^ (. +?) \< (. +?) \>$/', $toemail, $mats)? ($mailusername? ' =? '. $charset. '? B? '. Base64_encode ($mats [1]). "? = < $mats [2]> ": $mats [2]): $toemail;; $email _subject = ' =? '. $charset. '? B? '. Base64_encode (Preg_replace ("/[\r|\n]/", "$subject)"). = '; $email _message = Chunk_split (Base64_encode (str_replace ("\ n", "\ r \ n", Str_replace ("\ r", "\ n", Str_replace ("\ r \ n", " \ n ", Str_replace (" \n\r "," \ R ", $message)))))); $headers =" From: $email _from{$maildelimiter}x-priority:3{$ maildelimiter}x-mailer:phpcms-v9 {$maildelimiter}mime-version:1.0{$maildelimiter}content-type:text/html; Charset= ". $charset." {$maildelimiter}content-transfer-encoding:base64{$maildelimiter} "; if (! $fp = Fsockopen ($mail [' Server '], $mail [' Port '], $errno, $ ERRSTR) {runlog (' smtp ', "($mail [Server]: $mail [port]) connect-unable to CONNECT to the SMTP server", 0); return false ;} Stream_set_blocking ($fp, true); $lastmessage = Fgets ($fp, up to), if (substr ($lastmessage, 0, 3)! = ' n ') {runlog (' SMTP ', "$ Mail[server]: $mail [port] connect-$lastmessage ", 0); return false;} Fputs ($FP, ($mail [' auth ']? ' EHLO ': ' HELO '). "phpcms\r\n"); $lastmessage = Fgets ($FP); if (substr ($lastmessage, 0, 3)! = && substr ($las Tmessage, 0, 3)! =) {Runlog (' SMTP ', "($mail [Server]: $mail [port]) helo/ehlo-$lastmessage", 0); return false;} while (1) {if (substr ($lastmessage, 3, 1)! = '-' | | | empty ($lastmessage)) {break;} $lastmessage = fgets ($FP, 512);} if ($mail [' auth ']) {fputs ($fp, "auth login\r\n"); $lastmessage = Fgets ($FP); if (substr ($lastmessage, 0, 3)! = 334) {Run Log (' SMTP ', "($mail [Server]: $mail [port]) AUTH login-$lastmessage", 0); return fAlse;} Fputs ($fp, Base64_encode ($mail [' auth_username ']). " \ r \ n "); $lastmessage = Fgets ($fp, up to), if (substr ($lastmessage, 0, 3)! = 334) {runlog (' SMTP '," ($mail [Server]: $mail [port] ) username-$lastmessage ", 0); return false;} Fputs ($fp, Base64_encode ($mail [' Auth_password ']). " \ r \ n "); $lastmessage = Fgets ($fp, up to), if (substr ($lastmessage, 0, 3)! = 235) {runlog (' SMTP '," ($mail [Server]: $mail [port] ) password-$lastmessage ", 0); return false;} $email _from = $mail [' From '];} Fputs ($fp, "MAIL from: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $email _from)." $lastmessage = Fgets ($fp, >\r\n), if (substr ($lastmessage, 0, 3)! =) {fputs ($fp, "MAIL from: <". Preg_replac E ("/.*\< (. +?) \>.*/"," \\1 ", $email _from)." $lastmessage = Fgets ($fp, >\r\n), if (substr ($lastmessage, 0, 3)! =) {Runlog (' SMTP ', "($mail [Server]: $mail [ Port]) MAIL from-$lastmessage ", 0); return false;}} Fputs ($fp, "RCPT to: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $toemail)." >\r\n "); $lastmessage = fGets ($FP), if (substr ($lastmessage, 0, 3)! =) {fputs ($fp, "RCPT to: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $toemail)." $lastmessage = Fgets ($fp, >\r\n), Runlog (' SMTP ', "($mail [Server]: $mail [port]) RCPT to-$lastmessage", 0); return false;} Fputs ($fp, "data\r\n"); $lastmessage = Fgets ($fp, up to), if (substr ($lastmessage, 0, 3)! = 354) {runlog (' SMTP ', "($mail [ Server]: $mail [port]) data-$lastmessage ", 0); return false;} $headers. = ' Message-id: < '. Gmdate (' Ymdhs '). SUBSTR (MD5 ($email _message.microtime ()), 0, 6). Rand (100000, 999999). ' @ '. $_server[' Http_host ']. " >{$maildelimiter} "; Fputs ($fp," Date: ". Gmdate (' R ')." \ r \ n "); Fputs ($FP," to: ". $email _to." \ r \ n "); Fputs ($fp," Subject: ". $email _subject." \ r \ n "); Fputs ($fp, $headers." \ r \ n "); Fputs ($fp," \r\n\r\n "); Fputs ($fp," $email _message\r\n.\r\n "); $lastmessage = Fgets ($fp, +); if (substr ($ Lastmessage, 0, 3)! =) {Runlog (' SMTP ', "($mail [Server]: $mail [port]) end-$lastmessage", 0);} 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.