Home > Backend Development > PHP Tutorial > PHP sends emails in various forms (mail qmail mail system phpmailer class)_PHP tutorial

PHP sends emails in various forms (mail qmail mail system phpmailer class)_PHP tutorial

WBOY
Release: 2016-07-13 10:40:14
Original
965 people have browsed it

1. Use mail() function

There’s nothing much to say, just use the system’s built-in SMTP system to send, usually sendmail. This depends on each system. Use the reference manual.

2. Use pipe form

I just tested it successfully yesterday and used local qmail to send emails.

Copy code The code is 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 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 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 the pipe

return true;
}

------------------Test sending email:

//Send email

$subject = "Test Email";

$uid = $_POST['uid']; //from information
$content = "" .$u_email

." Hello!

Thank you, this email is for testing!"; //Content information

$u_email = "hren@yahoo.com.cn"; //Email sent to
if (send_check_mail($u_email, $subject, $uid, $content)) {

echo " Congratulations! Get voting emails sent to your inbox!

Please check your email: ".$u_email."

". $close;
} else {

echo "Unfortunately, sending the voting email to your mailbox failed. Please try again or contact the R&D staff.

". $close;

}

Of course, you can also use the same method to handle the sendmail process to send emails.

Code example below:

Copy code 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\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, this pipeline method is relatively low-level and depends on the stability of the program you call. So it is an optional way to send emails.


3. Use phpmailer class

is an open source email class, main website: http://phpmailer.sourceforge.net

There are two files inside, one is class.smtp.php, and the other is class.phpmailer.php
In addition, the usage method of the official website is added:
Examples using phpmailer
1. Advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.

Copy code 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 .= "Your personal 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\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"] . "
"; 

    // 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 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 = 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

复制代码 代码如下:

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 PEAR::Net_SMTP component

PEAR is really a good thing, and many people may not use it very much. At least I currently use its DB class, which is good for sending emails.

You need the Net_SMTP class, which can be downloaded from http://pear.php.net. The user manual of the Net_SMTP class:

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

I use the above classes, this one is the best, whether it is speed or anything else, but the operation involves some simple SMTP protocols.

My usage code:

Copy code The code is as follows:

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

require_once 'Net/SMTP.php'; //Load class library


$subject = "Test Email";

$uid = $_POST['uid']; //from information
$content = "" .$u_email

." Hello!

Thank you, this email is for testing!"; //Content information

$u_email = "hren@yahoo.com.cn"; //Email sent to

$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's address
$smtp->rcptTo($u_email); //Recipient's address
$date = date('r'); //Get the sending 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\r\n$content\r\n"); //Add sending data and send
$smtp->disconnect(); //Close the connection

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/726027.htmlTechArticle1. There is nothing to say about using the mail() function. It just uses the smtp system that comes with the system to send. Generally I use sendmail to send it. This depends on each system. Using the Reference Manual...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template