Php sends emails in multiple forms _ PHP Tutorial

Source: Internet
Author: User
Tags qmail
Php sends emails in multiple forms. This article mainly introduces php mail sending methods in various forms, including using the mail () function, using the pipeline, and using the phpmailer class. using m This article mainly introduces php mail sending methods in multiple forms, including using the mail () function, using the pipeline form, and using the phpmailer class. for details, refer to and use it.

1. there is nothing to say about using the mail () function, that is, using the system's built-in smtp system for sending, generally using sendmail for sending. This depends on different systems. User Reference Manual. 2. the test was successful yesterday in the form of MPs queue, and the local qmail was used to send emails. The code is as follows:/* use qmail to send an email function */function send_check_mail ($ email, $ subject, $ uid, $ buffer) {$ command = "/var/qmail/bin/qmail-inject ". $ email; // qmail program address, $ email is the address to be sent $ handle = popen ($ command, "w"); // open the pipeline http://www.cnblogs.com/roucheng/ if (! $ Handle) {return false;} $ from = "webmaster@unixsky.net"; // sender fwrite ($ handle, "From :". $ from. "\ n"); // write data to the pipeline fwrite ($ handle, "Return-Path :". $ from. "\ n"); fwrite ($ handle, ":". $ 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 the pipeline return true;} ---------------- test sent mail: // send mail $ subject = "test mail "; $ uid = $ _ POST ['uid']; // from information $ content ="". $ U_email." Hello!

Thank you for testing this email! "; // 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 email to your mailbox!

Please check your email: ". $ u_email ."

". $ Close;} else {echo" unfortunately, failed to send a voting email to your mailbox. please try again or contact R & D personnel.

". $ Close;} of course, the same method can also be used to process sendmail processes to send emails. The following code is used as an example: In fact, the method of this pipeline is relatively low, depending on the stability of the program you call. Therefore, it is an optional email sending method. 3. using phpmailer class is an open source mail class, the main site: http://phpmailer.sourceforge.net contains two files, one is class. smtp. php, but also a class. phpmailer. php also adds the official website usage: Examples using phpmailer 1. advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support. the code is as follows: require ("class. phpmailer. php "); $ mail = new phpmailer (); $ mail-> From =" list@example.co M "; $ 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". $ row ["full_name"]. ",

"; $ Body. ="YourPersonal photograph to this message.

"; $ Body. =" Sincerely,
"; $ Body. = "phpmailer List manager"; // Plain text body (for mail clients that cannot read HTML) $ text_body = "Hello ". $ row ["full_name"]. ", \ n"; $ text_body. = "Your personal photograph to this message. \ 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-> AddSt RingAttachment ($ row ["photo"], "YourPhoto.jpg"); if (! $ Mail-> Send () echo "There has been a mail error sending to". $ row ["email"]."
"; // Clear all addresses and attachments for next loop $ mail-> ClearAddresses (); $ mail-> ClearAttachments ();} 2. extending phpmailerExtending classes with inheritance is one of the most powerful features of object-oriented programming. it allows you to make changes to the original class for your own personal use without hacking the original classes. plus, it is very easy to do. I 'VE provided an exa Mple: Here's a class that extends the phpmailer class and sets the defaults for the participating site: PHP include file: mail. inc. the php code is as follows: require ("class. phpmailer. php "); code: class my_phpmailer extends phpmailer {// Set default variables for all new objects var $ From =" from@example.com "; var $ FromName =" Mailer "; var $ Host = "smtp1.example.com; smtp2.example.com"; var $ Mailer = "smtp"; // Alternat Ive to IsSMTP () var $ WordWrap = 75; // Replace the default error_handler function error_handler ($ msg) {print ("My Site Error"); print ("Description: "); printf (" % s ", $ msg); exit;} // Create an additional function do_something ($ something) {// Place your new code here} Now's a normal PHP page in the site, which will have all the defaults set above: Normal PHP file: mail_test.php code as shown in Under: require ("mail. inc. php "); // Instantiate your new class $ mail = new my_phpmailer; // Now you only need to add the necessary stuff $ mail-> AddAddress (" josh@example.com ", "Josh Adams"); $ mail-> Subject = "Here is the subject"; $ mail-> Body = "This is the message body "; $ mail-> AddAttachment ("c:/temp/11-10-00.zip", "new_name.zip"); // optional name if (! $ Mail-> Send () {echo "There was an error sending the message"; exit;} echo "Message was sent successfully"; 4. using PEAR: The Net_SMTP component PEAR is really a good thing. many people may not use it very much. at least I currently use its DB class, and the Mail class is good. Net_SMTP class is required. http://pear.php.net Download the Net_SMTP class User Manual: http://pear.php.net /Manual/en/package.networking.net-smtp. php I use the above classes. this is the best, regardless of the speed or other, but the operation involves some simple smtp protocols. My code: the code is as follows: // -------------------------------------------- require_once 'net/SMTP. php '; // load the class library $ subject = "test email"; $ uid = $ _ POST ['uid']; // from information $ content ="". $ U_email." Hello!

Thank you for testing this email! "; // Content information $ u_email =" hren@yahoo.com.cn "; // sent to the mailbox $ smtp = new Net_SMTP ('2017. 168.0.1 '); // smtp server $ smtp-> connect (); // connect to the server $ smtp-> helo ('unixsky. net '); // send HELO information to the server $ smtp-> mailFrom ('hren @ unixsky.net'); // sender address $ smtp-> rcp.pdf ($ u_email ); // recipient address $ date = date ('r'); // Obtain the Mail 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 $ content \ r \ n "); // add and send data $ smtp-> disconnect (); // close the connection

For more information about using the pipeline () function, using the pipeline form, and using the phpmailer class, see use it. 1. use m...

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.