Php sends emails in multiple forms (the phpmailer class in the mailqmail mail system) _ PHP Tutorial

Source: Internet
Author: User
Tags qmail
Php sends emails in multiple forms (the phpmailer class in the mailqmail mail system ). 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. Reference Manual 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.

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 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 ="". $ U_email

. "Hello!

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

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:


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

The 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:

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"; // 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

Http://www.bkjia.com/PHPjc/726027.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/726027.htmlTechArticle1. the use of mail () function is nothing to say, is to use the system's built-in smtp system to send, generally use sendmail to send. This depends on different systems. Reference Manual...

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.