Use php mail to send email code

Source: Internet
Author: User
Tags foreach auth explode mail code

This php e-mail code uses php's free email sending function mail to send emails. He will use two methods to determine whether the mail function is available, if the row uses it to send it, otherwise fsockopen will be used for operation.
*/

The code is as follows: Copy code

Class email
{
Function email ()
 {
$ This->__ construct ();
 }

Function _ construct ()
    {
@ Define ('charset', 'gbk ');
$ This-> set (mail_server, mail_port, mail_user, mail_pwd, mail_type );
$ This-> auth = 1;
 }

Function set ($ server, $ port, $ user, $ password, $ type = 1, $ delimiter = 1, $ mailusername = 0)
 {
$ This-> type = $ type;
$ This-> server = $ server;
$ This-> port = $ port;
$ This-> user = $ user;
$ This-> password = $ password;
$ This-> delimiter = $ delimiter = 1? "Rn": ($ delimiter = 2? "R": "n ");
$ This-> mailusername = $ mailusername;
 }
 
Function send ($ email_to, $ email_subject, $ email_message, $ email_from = '')
 {
Global $ dircms;
$ Email_to = iconv ("UTF-8", "gbk", $ email_to );
$ Email_subject = iconv ("UTF-8", "gbk", $ email_subject );
$ Email_message = iconv ("UTF-8", "gbk", $ email_message );
$ Email_subject = '=? '. Charset .'? B? '. Base64_encode (str_replace ("r", '', $ email_subject )).'? = ';
$ Email_message = str_replace ("rn. "," rn .. ", str_replace (" n "," rn ", str_replace (" r "," n ", str_replace (" rn "," n ", str_replace (" nr ", "r", $ email_message )))));
$ Adminemail = $ this-> type = 1? $ Dircms ['mail _ user']: $ dircms ['mail _ user'];
$ Email_from = ''? '=? '. Charset .'? B? '. Base64_encode ($ dircms ['site _ name']). "? = <$ Adminemail> ": (preg_match ('/^ (. + ?) <(. +?)> $/', $ Email_from, $ from )? '=? '. Charset .'? B? '. Base64_encode ($ from [1]). "? = <$ From [2]> ": $ email_from );
$ Emails = explode (',', $ email_to );
Foreach ($ emails as $ touser)
  {
$ Tousers [] = preg_match ('/^ (. + ?) <(. +?)> $/', $ Touser, $ )? ($ This-> mailusername? '=? '. Charset .'? B? '. Base64_encode ($ to [1]). "? = <$ To [2]> ": $ to [2]): $ touser;
  }
$ Email_to = implode (',', $ tousers );
$ Headers = "from: $ email_from {$ this-> delimiter} x-priority: 3 {$ this-> delimiter} x-mailer: dircms {$ this-> delimiter} mime-version: 1.0 {$ this-> delimiter} content-type: text/html; charset = ". charset. "{$ this-> delimiter }";
If ($ this-> type = 1)
  {
Return $ this-> smtp ($ email_to, $ email_subject, $ email_message, $ email_from, $ headers );
  }
Elseif ($ this-> type = 2)
  {
Return @ mail ($ email_to, $ email_subject, $ email_message, $ headers );
  }
Else
  {
Ini_set ('smtp ', $ this-> server );
Ini_set ('smtp _ port', $ this-> port );
Ini_set ('sendmail _ from', $ email_from );
Return @ mail ($ email_to, $ email_subject, $ email_message, $ headers );
  }
 }

Function smtp ($ email_to, $ email_subject, $ email_message, $ email_from = '', $ headers = '')
 {
Global $ dircms;
If (! $ Fp = fsockopen ($ this-> server, $ this-> port, $ errno, $ errstr, 10 ))
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) connect-unable to connect to the smtp server", 0 );
Return false;
  }
Stream_set_blocking ($ fp, true );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = '000000 ')
  {
$ This-> errorlog ('smtp ', "$ this-> server: $ this-> port connect-$ lastmessage", 0 );
Return false;
  }
Fputs ($ fp, "ehlo dircmsrn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 220 & substr ($ lastmessage, 0, 3 )! = 250)
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) helo/ehlo-$ lastmessage", 0 );
Return false;
  }
While (1)
  {
If (substr ($ lastmessage, 3, 1 )! = '-' | Empty ($ lastmessage ))
   {
Break;
   }
$ Lastmessage = fgets ($ fp, 512 );
  }
Fputs ($ fp, "auth loginrn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334)
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) auth login-$ lastmessage", 0 );
Return false;
  }
Fputs ($ fp, base64_encode ($ this-> user). "rn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 334)
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) username-$ lastmessage", 0 );
Return false;
  }
Fputs ($ fp, base64_encode ($ this-> password). "rn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 235)
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) password-$ lastmessage", 0 );
Return false;
  }
Fputs ($ fp, "mail from: <". preg_replace ("/. * <(. +?)>. */"," ", $ Email_from)."> rn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250)
  {
Fputs ($ fp, "mail from: <". preg_replace ("/. * <(. +?)>. */"," ", $ Email_from)."> rn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250)
   {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) mail from-$ lastmessage", 0 );
Return false;
   }
  }
$ Email_tos = array ();
$ Emails = explode (',', $ email_to );
Foreach ($ emails as $ touser)
  {
$ Touser = trim ($ touser );
If ($ touser)
   {
Fputs ($ fp, "rcpt to: <". preg_replace ("/. * <(. +?)>. */"," ", $ Touser)."> rn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 250)
    {
Fputs ($ fp, "rcpt to: <". preg_replace ("/. * <(. +?)>. */"," ", $ Touser)."> rn ");
$ Lastmessage = fgets ($ fp, 512 );
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) rcpt to-$ lastmessage", 0 );
Return false;
    }
   }
  }
Fputs ($ fp, "datarn ");
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = 354)
  {
$ This-> errorlog ('smtp ', "($ this-> server: $ this-> port) data-$ lastmessage", 0 );
  }
$ Headers. = 'message-id: <'. gmdate ('ymmdhs '). '. '. substr (md5 ($ email_message.microtime (), 0, 6 ). rand (100000,999 999 ). '@'. $ _ server ['http _ host']. ">{$ this-> delimiter }";
Fputs ($ fp, "date:". gmdate ('r'). "rn ");
Fputs ($ fp, "to:". $ email_to. "rn ");
Fputs ($ fp, "subject:". $ email_subject. "rn ");
Fputs ($ fp, $ headers. "rn ");
Fputs ($ fp, "rnrn ");
Fputs ($ fp, "$ email_messagern.rn ");
Fputs ($ fp, "quitrn ");
Return true;
 }

Function errorlog ($ type, $ message, $ is)
 {
$ This-> error [] = array ($ type, $ message, $ is );
 }
}

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.