php Send mail in multiple forms

Source: Internet
Author: User
Tags comments manual pear php file php send mail require zip mysql database

  This article mainly introduces the various forms of PHP to send mail, including the use of mail () function, the use of pipe form, the use of Phpmailer class and other methods, we refer to the use of the bar

1. Use the mail () function     Nothing to say, is to use the system with the SMTP system to send, generally use sendmail to hair. This varies according to each system. Use the reference manual.     2. Using the form of a pipe     was successfully tested yesterday, using local qmail to send mail.     Code is as follows:/* use qmail to send mail functions/  function Send_check_mail ($email, $subject, $uid, $buffer)   {  &nbs p; $command =   "/var/qmail/bin/qmail-inject". $email; QMail program address, $email is the address to send     $handle = Popen ($command, "w"); Open pipe  http://www.cnblogs.com/roucheng/ if (! $handle) {    return false;    }  &nbs P     $from = "webmaster@unixsky.net"; Sender    fwrite ($handle, "from:". $from. " \ n "); Write data to the pipe    fwrite ($handle, "Return-path:". $from. " \ n ");    fwrite ($handle, "to:". $uid. " \ n ");    fwrite ($handle, "Subject:". $subject. " \ n ");    fwrite ($handle, "mime-version:1.0\n");    fwrite ($handle, "content-type:text/html; Charset=\ "gb2312\" "\ n");    fwrite ($handle, $buffer. " \ n ");    pcloSE ($handle); Close pipe      return true;      ------------------test Send message:    //Send mail     $subject = "test Email";     $uid = $_post[' uid ']; From information   $content = "<html><body>" $u _email          "Hello! <br><br> Thank you, this email test! <br</body></html> "; Content information     $u _email = "hren@yahoo.com.cn"; Sent to the mailbox   if (Send_check_mail ($u _email, $subject, $uid, $content)) {     echo "Congratulations! Send a voting message to your mailbox! <br><br> Please check your email: <font color= #CC0033 > $u _email. "</font><br><br>". $close;   &NBSP} else {     echo "Unfortunately, sending a voting message to your mailbox failed, please try again or contact the developer." <br><br> ". $close;    }     Of course, you can use the same method to process SendMail processes to send mail.     The following code example:      code is as follows: <?php   $PP = Popen ("/usr/sbin/sendmail-t", "w") or Die ("Cannot fo RK SendMail ");   Fputs ($pp, "to:sterling@designmultimedia.com\r\ n ");   Fputs ($pp, "reply-to: $senders _email\r\n");   Fputs ($PP, "From: $senders _email\r\n");   Fputs ($pp, "Subject the Results of your form\r\n\r\n");   Fputs ($pp, "$senders _email sent fllowing comments:\r\n");   Fputs ($pp, $comments);   Pclose ($PP) or Die ("cannot close pipe to SendMail");  ?>     In fact, this kind of pipeline method is relatively low-level, depending on the stability of the program you call. So it's an optional way to send a message.       3. Use the Phpmailer class     is an open source Send mail class, main station:http://phpmailer.sourceforge.net    inside is two files, One is class.smtp.php, and the other is class.phpmailer.php plus the official web site using the method:  examples using phpmailer  1. Advanced Examplethis demonstrates sending out multiple email messages and binary attachments from a MySQL database with M Ultipart/alternative support.   Code as follows: Require ("class.phpmailer.php");     $mail = new Phpmailer ();     $mail->from     = "list@example.com";   $mail->fromname = "List manager";   $mail->host     = "SMTP1.example.com;smtp2.example.com ";   $mail->mailer   = "SMTP";     @MYSQL_CONNECT ("localhost", "root", "password");   @mysql_select_db ("My_company");   $query? SELECT full_name, Email,?hoto? ROM employee? here?d= $id ";   $result?? mysql_query ($query);     while ($row = Mysql_fetch_array ($result))   {     //HTML body       $ Body  = "Hello <font size=\" 4\ ">". $row ["Full_name"]. "</font&gt, <p>";       $body. = "<i>Your</i> personal photograph to this message.<p>";       $body. = "Sincerely, <br>";       $body. = "Phpmailer List manager";        //Plain text body (for mail clients that cannot read HTML)       $text _body &N Bsp;= "Hello". $row ["Full_name"]. ", \ n";       $text _body. = "Your personal photograph to this message.\n\n";       $text _body . = "sincerely,\n";       $text _body. = "Phpmailer List manager";         $mail->body    = $body;       $mail->altbody = $text _body;       $mail->addaddress ($row ["email"], $row ["Full_name"]);       $mail->addstringattachment ($row ["Photo"], "yourphoto.jpg");         if (! $mail->send ())           echo "There has been a mail error sen Ding to ". $row ["Email"]. "<br>";        /Clear all addresses with attachments for Next loop       $mail->clearaddre SSEs ();       $mail->clearattachments ();  }     2. Extending phpmailerextending classes with inheritance is one of the most powerful features of object-oriented. It allows the changes to "original class for your own personal use without the hacking original. Plus, it is very easy to do.I ' ve provided a example:    Here's class that extends the Phpmailer class and sets the defaults for the Parti Cular site:  PHP include file:mail.inc.php  code is as follows: Require ("class.phpmailer.php");      code is as follows: Class My_phpmailer extends Phpmailer {     //Set default variables for all new objects     &NB Sp var $From     = "from@example.com";       var $FromName = "Mailer";       var $Host     = "smtp1.example.com;smtp2.example.com";       var $Mailer   = "SMTP";                        //Alternative to ISSMTP ()   &nbs P   var $WordWrap = 75;        //Replace The default Error_Handler       function Error_Handler ($msg) {&nbsp ;         print ("My Site Error");           print ("Description:");           printf ("%s", $msg);           exit;      }        //Create an additional function       function Do_somet Hing ($something) {         /Place your new code here      }  }   & nbsp Now here's a normal PHP page in the site, which'll have all the defaults set above:  normal PHP file:mail_test.php     Code as follows: Require ("mail.inc.php");    //Instantiate your new class   $mail = new My_phpmailer;    //now necessary stuff   $mail->addaddress ("josh@example.com", "Josh Adams") need to add the );   $mail->subject = "This is the Subject";   $mail->body    = "This are the message body";   $mail->addattachment ("C:/temp/11-10-00.zip", "New_name.zip");  //Optional name     if (! $mail->send ())   {     echo "There was a error Sending the message ";      exit;  }     echo "message is sent successfully";     4. Using the PEAR::NET_SMTP component     Pear is really a good thing, probably very many people do not how to use, at least I currently use his DB class, send the Mail class is good.     Need NET_SMTP class, can go to http://pear.php.net download, NET_SMTP class manual:    http://pear.php.net/manual/en/ package.networking.net-smtp.php    I use the above several classes, this is the best, whether speed or anything else, but the operation involves some simple SMTP protocol.     My use code:     Code is as follows://------------------------------------------    require_once ' net/ Smtp.php '; Load class Library        $subject = "Test Mail";     $uid = $_post[' uid ']; From information   $content = "<html><body>" $u _email          "Hello! <br><br> Thank you, this email test! <br</body></html> "; Content information     $u _email = "hren@yahoo.com.cn"; Sent to the mailbox     $SMTP = new Net_smtp (' 192.168.0.1 '); SMTP server   $SMTP->connect (); Connect server   $SMTP->helo (' unixsky.net '); Send helo information to server &nbsP $smtp->mailfrom (' hren@unixsky.net '); Sender address   $SMTP->rcptto ($u _email); Recipient address   $date = date (' R '); Get date   $SMTP->data ("Date: $date \r\nfrom:vdddote@eyou.net\r\nto: $u _email\r\nsubject: $ subject\r\ncontent-type:text/html; Charset=\ "gb2312\" \r\n\r\n$content\r\n "); Add send data and send   $SMTP->disconnect (); Close connection  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.