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.
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.
1. Use the mail () function
There is nothing to mention, that is, sending via the smtp system that comes with the 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.
The 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 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 ="". $ U_email
. "Hello!
Thank you for testing this email!
< body> "; // Content information
$ 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!
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 example:
The Code is as follows:
$ 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 email sending class, main site:
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.
The 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". $ 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-> AddStringAttachment ($ 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 example:
Here's a class that extends the phpmailer class and sets the defaults for the participating site:
PHP include file: mail. inc. php
The Code is as follows:
Require ("class. phpmailer. php ");
The Code is as follows: