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.
Copy codeThe Code is as follows:
/* 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 pipe 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, "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 = "
. "Hello! <Br> Thank you for testing this email! <Br </body>
$ U_email = "hren@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 a voting email to your mailbox. Please try again or contact R & D personnel. <Br> ". $ close;
}
Of course, the same method can also be used to process sendmail processes to send emails.
The following code example:
Copy codeThe Code is as follows:
<? 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 ");
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.
In addition, the use of the official website is as follows:
Examples using phpmailer
1. Advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.
Copy codeThe Code is 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>, <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 to". $ row ["email"]. "<br> ";
// 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 example:
Here's a class that extends the phpmailer class and sets the defaults for the participating site:
PHP include file: mail. inc. php
Copy codeThe Code is as follows:
Require ("class. phpmailer. php ");
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
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. Use the PEAR: Net_SMTP component
PEAR is really a good thing. It may not be very useful to many people. At least I currently use its DB class, 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:
Copy codeThe Code is as follows:
//------------------------------------------
Require_once 'net/SMTP. php'; // load the class library
$ Subject = "test email ";
$ Uid = $ _ POST ['uid']; // from information
$ Content = "
. "Hello! <Br> Thank you for testing this email! <Br </body>
$ U_email = "hren@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 ('hren @ unixsky.net '); // sender address
$ Smtp-> rcp.pdf ($ u_email); // recipient address
$ Date = date ('R'); // get 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