A summary of several ways to send mail in PHP

Source: Internet
Author: User
Tags pear qmail

1. Use the Mail () function

Nothing to say, is to use the system's own SMTP system to send, is generally used sendmail to hair. This varies according to each system. Use the reference manual.

2. Use the form of piping

The test was successful yesterday, using a local qmail to send mail.


/* Use qmail to send mail function * *
function Send_check_mail ($email, $subject, $uid, $buffer)
{
$command = "/var/qmail/bin/qmail-inject". $email; QMail program address, $email is the address to send
$handle = Popen ($command, "w"); Open pipe
if (! $handle) {
return false;
}

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

. "Hello." <br><br> Thank you for this email test. <br</body>

$u _email = "heiyeluren@yahoo.com.cn"; Sent to the mailbox
if (Send_check_mail ($u _email, $subject, $uid, $content)) {

echo "Congratulations. Send a voting message to your mailbox. <br><br> Please check your email: <font color= #CC0033 > $u _email. "</font><br><br>". $close;
} else {

echo "Unfortunately, sending a voting message to your mailbox failed, please try again or contact the developer." <br><br> ". $close;

}

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

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 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-level, depending on the stability of the program you call. So it's an optional way to send a message.

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, and the other is class.phpmailer.php, using the method can refer to the following article:

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

Plus the use of the official website: examples using Phpmailer 1. Advanced Example

This is demonstrates sending out multiple email messages with binary attachments from a MySQL database with Multipart/alterna tive 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, sham Hoto braised ROM employee 燱 here 爄 d= $id ";

$result 燖 mysql_query ($query); while ($row = Mysql_fetch_array ($result)) {//HTML body $body = "Hello <font size=/" 4/">". $row ["Full_na Me "].
    "</font&gt, <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/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 has been a mail error sending to". $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. It allows the changes to "original class for your own personal use without the hacking original. 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 particular 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 =;

    Replace The default Error_Handler
    function Error_Handler ($msg) {
        print ("My Site Error");
        Print ("Description:");
        printf ("%s", $msg);
        Exit;
    }

    Create a additional function
    function do_something ($something) {
        //Place your new code here
    }
}

Now here's a normal PHP page in the site, which'll 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 I need to add the necessary stuff
$mail->addaddress ("josh@example.com", "Josh Adams");
$mail->subject = "Here is the Subject";
$mail->body    = "This are the message body";
$mail->addattachment ("C:/temp/11-10-00.zip", "New_name.zip");  Optional name

if (! $mail->send ())
{
   echo "There was a error sending the message";
   Exit;
}

echo "message is sent successfully";

4. Using the PEAR::NET_SMTP component

Pear is really a good thing, probably many people do not how to use, at least I now use its DB class, send the Mail class is good.

Need to NET_SMTP class, you can go to http://pear.php.net download, NET_SMTP class use manual:

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

I use a few of the above classes, this is the best, whether it is speed or something else, but the operation involves some simple SMTP protocol.

My Code of Use:

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

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

$subject = "Test Mail";

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

. "Hello." <br><br> Thank you for this email test. <br</body>

$u _email = "heiyeluren@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 (' heiyeluren@unixsky.net '); Sender Address
$smtp->rcptto ($u _email); Recipient address
$date = Date (' R '); Get the date of the letter
$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 send data and send
$SMTP->disconnect (); Close connection

5. Other methods

For example, use the socket, write your own program.

The Pear::mail class is not the same as the above, I have not made, you can try.

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.