Summary of several methods for sending emails in PHP

Source: Internet
Author: User
Tags pear qmail
Document directory
  • 1. Advanced example
  • 2. Extending phpmailer

1. Use the mail () function

There is nothing to mention, that is, sending via the system's built-in SMTP system, generally using Sendmail. This depends on different systems. User reference manual.

2. MPS queue format

The test was successful yesterday and the local Qmail was used to send emails.

/* Function for sending emails using Qmail */
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
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, "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 the MPs queue

Return true;
}

------------------ Test email sending:

// Send an email

$ Subject = "test email ";

$ Uid = $ _ post ['uid']; // from information
$ Content = "<HTML> <body>". $ u_email

. "Hello! <Br> Thank you for testing this email! <Br </body>

$ U_email = "heiyeluren@yahoo.com.cn"; // email
If (send_check_mail ($ u_email, $ subject, $ uid, $ content )){

Echo "Congratulations! Send a voting email to your mailbox! <Br> check your email address: <font color = # cc0033> ". $ u_email." </font> <br> ". $ close;
} Else {

Echo "Unfortunately, failed to send the voting email to your mailbox. Please try again or contact the developer. <Br> ". $ close;

}

Of course, you can also use the same method to process the Sendmail process to send emails.

The following code example:

<? PHP
$ Pp = popen ("/usr/sbin/sendmail-T", "W") or die ("cannot fork 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 the fllowing comments:/R/N ");
Fputs ($ PP, $ comments );
Pclose ($ pp) or die ("cannot close pipe to Sendmail ");
?>

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. Use the phpmailer class

Is an open source mail class, main site: http://phpmailer.sourceforge.net

There are two files, one is class. SMTP. php, and the other is class. phpmailer. php. The usage can be found in the following article:

Http://blog.jianqing.net/2005/05/02/201-phpmailer

In addition, the use of the official website is as follows:

Examples using phpmailer1. advanced example

This demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.

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, transferhoto employee Rom employee here author d = $ id "; $ result? Define mysql_query ($ query); While ($ ROW = mysql_fetch_array ($ result) {// html body $ body = "Hello <font size =/" 4/"> ". $ row ["full_name"]. "</font>, <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 = "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-> addstringattachment ($ row ["photo"], "yourphoto.jpg"); If (! $ Mail-> send () echo "There has been a mail error sending ". $ row ["email"]. "<br>"; // clear all addresses and attachments for next loop $ mail-> clearaddresses (); $ mail-> clearattachments ();}
2. Extending phpmailer

Extending 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 example:

Here's a class that extends the phpmailer class and sets the defaults for the participating site:
PHP Include File:Mail. Inc. php

require("class.phpmailer.php");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";                         // Alternative 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    function do_something($something) {        // Place your new code here    }}

Now here's a normal PHP page in the site, which will have all the defaults set above:
Normal PHP file:Mail_test.php

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 nameif(!$mail->Send()){   echo "There was an error sending the message";   exit;}echo "Message was sent successfully";

4. Use the pear: net_smtp component

Pear is really a good thing, and many people may not use it very much. At least I use its dB class now, and the mail class is good.

Need net_smtp class, can go to the http://pear.php.net download, net_smtp class user manual:

Http://pear.php.net/manual/en/package.networking.net-smtp.php

I use the above several classes. This is the best, whether it is speed or something else, but the operation involves some simple SMTP protocols.

My code:

//------------------------------------------

Require_once 'net/SMTP. php'; // load the class library

$ Subject = "test email ";

$ Uid = $ _ post ['uid']; // from information
$ Content = "<HTML> <body>". $ u_email

. "Hello! <Br> Thank you for testing this email! <Br </body>

$ U_email = "heiyeluren@yahoo.com.cn"; // email

$ SMTP = new net_smtp ('192. 168.0.1 '); // SMTP Server
$ SMTP-> connect (); // connect to the server
$ SMTP-> Helo ('unixsky. net'); // send helo information to the server
$ SMTP-> mailfrom ('heiyeluren @ unixsky.net '); // sender address
$ SMTP-> rcp.pdf ($ u_email); // recipient address
$ Date = Date ('R'); // get the mail date
$ SMTP-> data ("Date: $ date/R/nfrom: vote@eyou.net/R/nto: $ u_email/R/nsubject: $ subject/R/ncontent-type: text/html; charset =/"gb2312/"/R/n/R/N $ content/R/N "); // Add and send data
$ SMTP-> disconnect (); // close the connection

5. Other Methods

For example, you can use socket to write your own program.

Pear: The Mail class is different from the preceding one. I have never used it. Please try it.

Other methods...

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.