php Various forms to send mail of mail qmail mail system phpmailer class

  • 2020-12-16 05:54:15
  • OfStack

Use the mail() function

There is nothing to talk about, it is to use the smtp system that comes with the system to send, 1 generally use sendmail to send. This varies from system to system. Use the reference manual.

2. Use a pipe

The test was successful yesterday and the local qmail was used to send the email.


/*  use qmail Send mail function  */  
function send_check_mail($email, $subject,$uid,$buffer)  
{  
 $command =  "/var/qmail/bin/qmail-inject ".$email; //qmail The program address ,$email Is the address to send   
 $handle = popen($command, "w"); // Open the pipe   http://www.cnblogs.com/roucheng/
 if (!$handle) {  
  return false;  
 }   

 $from = "webmaster@unixsky.net"; // The 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 pipeline   

 return true;  
}  

------------------ Test send email:   

// Send E-mail   

$subject = " Test email ";  

$uid = $_POST['uid']; //from information   
$content = "<html><body>".$u_email   

   ."  Hello! <br><br> Thank you, this email test! <br</body></html>"; // Content information   

$u_email = "hren@yahoo.com.cn"; // To the mailbox   
if (send_check_mail($u_email, $subject, $uid, $content)) {  

 echo " A: congratulations! Send a poll email to your inbox! <br><br> Please check your email address: <font color=#CC0033>".$u_email." </font><br><br>". $close;  
 } else {  

 echo " Unfortunately, sending the voting email to your mailbox failed, please try again or contact the r&d staff. <br><br>". $close;  

}

Of course, you can use the same method to process the sendmail process to send mail.

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 the fllowing comments: \ r \ n");  
fputs($pp, $comments);  
pclose($pp) or die("Cannot close pipe to sendmail");  
?>

In fact, this kind of pipeline method is lower level, depending on the stability of the program you call. So it's an optional way to send an email.


3. Use the phpmailer class

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

There are two files inside, one is ES36en.smtp.php, and another is class.phpmailer.php
In addition, the usage 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.


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

PEAR is such a good thing that probably a lot of people don't use it. At least I'm using his DB class at the moment, which is good for sending emails.

Need Net_SMTP class, can go to http: / / pear php. net download, Net_SMTP class manual:

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

I use the above classes, and this is the best one, either for speed or otherwise, but the operation involves some simple smtp protocol.

My usage code:


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

require_once 'Net/SMTP.php'; // To load the class library   

  
$subject = " Test email ";  

$uid = $_POST['uid']; //from information   
$content = "<html><body>".$u_email   

   ."  Hello! <br><br> Thank you, this email test! <br</body></html>"; // Content information   

$u_email = "hren@yahoo.com.cn"; // To the mailbox   

$smtp = new Net_SMTP('192.168.0.1'); //smtp The server   
$smtp->connect(); // Connect to server   
$smtp->helo('unixsky.net'); // send HELO Information to server   
$smtp->mailFrom('hren@unixsky.net'); // Sender address   
$smtp->rcptTo($u_email); // Recipient address   
$date = date('r'); // Get the date of dispatch   
$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 the connection 


Related articles: