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.
Copy Code code as follows:
/* 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 http://www.cnblogs.com/roucheng/
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");
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, this email test! <br</body>
$u _email = "hren@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:
Copy Code code 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\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 are calling. 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
Plus the official website usage:
Examples using Phpmailer
1. Advanced Examplethis demonstrates sending out multiple email messages and binary attachments from a MySQL database wit H multipart/alternative Support.
Copy 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 <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\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 Programmi Ng. 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
Copy Code code as follows:
Require ("class.phpmailer.php");
Copy 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 have all the defaults set above:
Normal PHP file:mail_test.php
Copy Code code as follows:
Require ("mail.inc.php");
Instantiate your new class
$mail = new My_phpmailer;
Now you are 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 very many people do not how to use, at least I currently use his DB class, Send mail class is good.
Need NET_SMTP class, 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:
Copy Code code as follows:
//------------------------------------------
Require_once ' net/smtp.php '; Load Class Library
$subject = "Test Mail";
$uid = $_post[' uid ']; From information
$content = "
. "Hello!" <br><br> Thank you, this email test! <br</body>
$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 the date of the letter
$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