PHP multiple forms of sending mail (mail qmail mail system Phpmailer Class) _php tutorial

Source: Internet
Author: User
Tags pear qmail
1. Using the Mail () function

There is nothing to talk about, is to use the system comes with the SMTP system to send, usually using sendmail to hair. This varies by system. Use the reference manual.

2. Use the form of a pipe

The test was successful yesterday, using the local qmail to send the message.

Copy CodeThe code is as follows:
/* Send mail function 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 Pipe http://www.cnblogs.com/roucheng/
if (! $handle) {
return false;
}

$from = "Webmaster@unixsky.net"; Sender
Fwrite ($handle, "from:". $from. " \ n "); Writing data to Pipelines
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 pipe

return true;
}

To send a message------------------test:

Send mail

$subject = "Test Mail";

$uid = $_post[' uid ']; From information
$content = "". $u _email

. "Hello!

Thank you, this email test! <>< body=""> "; 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 poll email to your email!

Please check your e-mail: ". $u _email."

". $close;
} else {

echo "Unfortunately, sending a poll message to your mailbox failed, please retry or contact the developer.

". $close;

}
<>

Of course, you can also use the same method to process SendMail processes to send messages.

The following code example:

Copy the Code code 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\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 lower, depending on the stability of the program you call. So it's an optional way to send mail.


3. Using the Phpmailer class

is an open source Send mail class, main station: http://phpmailer.sourceforge.net

There are two files, one is class.smtp.php, the other is class.phpmailer.php
In addition, the official website uses the method:
Examples using Phpmailer
1. Advanced Examplethis demonstrates sending out multiple e-mail messages with binary attachments from a MySQL database wit H multipart/alternative Support.

Copy the Code code 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. = "Your personal photograph to the 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\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 have 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 are one of the most powerful features of object-oriented Programmi Ng. It allows changes to the original class for your own personal use without hacking the original classes. Plus, it's very easy-to-do. I ' ve provided an example:

Here's a class that extends the Phpmailer class and sets the defaults for the particular site:
PHP include file:mail.inc.php
Copy the Code code as follows:
Require ("class.phpmailer.php");

Copy the Code code 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'll has all the defaults set above:
Normal PHP file:mail_test.php

Copy the Code code as follows:
Require ("mail.inc.php");

Instantiate your new class
$mail = new My_phpmailer;

Now you have 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 is an error sending the message";
Exit
}

echo "Message was sent successfully";

4. Using the PEAR::NET_SMTP component

Pear is really a good thing, probably very many people do not use, at least I currently use his DB class, Send mail class are good.

Need NET_SMTP class, can go to 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 classes, this is the best, regardless of speed or anything else, but the operation involves some simple SMTP protocol.

My Code of Use:

Copy the Code code as follows:
//------------------------------------------

Require_once ' net/smtp.php '; Load Class Library


$subject = "Test Mail";

$uid = $_post[' uid ']; From information
$content = "". $u _email

. "Hello!

Thank you, this email test! <>< body=""> "; Content Information

$u _email = "hren@yahoo.com.cn"; Sent to the mailbox

$SMTP = new Net_smtp (' 192.168.0.1 '); SMTP server
$SMTP->connect (); Connecting to a server
$smtp->helo (' unixsky.net '); Send helo information to the server
$smtp->mailfrom (' hren@unixsky.net '); Sender Address
$smtp->rcptto ($u _email); Recipient address
$date = Date (' R '); Get date of sending
$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\r\n$content\r\n "); Add send data and send
$SMTP->disconnect (); Close connection
<>

http://www.bkjia.com/PHPjc/726027.html www.bkjia.com true http://www.bkjia.com/PHPjc/726027.html techarticle 1. Using the mail () function is nothing to say, is to use the system comes with the SMTP system to send, usually using the sendmail to hair. This varies by system. Use the reference manual ...

  • Related Article

    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.