Send emails in various forms with php_PHP tutorial
This article mainly introduces various methods of sending emails in PHP, including using the mail() function, using pipes, using the phpmailer class, etc. Please refer to it for reference
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 the form of pipeline I just successfully tested it yesterday and used local qmail to send emails. 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! A voting email will be sent to your email!
Please check your email: ".$u_email."
< ;br>". $close; } else { echo "Unfortunately, sending the voting email to your mailbox failed. Please try again or contact the development staff.
". $close; } Of course, you can also use the same method to handle the sendmail process to send emails. Code example below: The code is as follows: 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 It is an open source email class. Main site: http://phpmailer.sourceforge.net There are two files inside, one is class.smtp.php, and the other is class.phpmailer.php In addition, how to use the official website: Examples using phpmailer 1. Advanced ExampleThis demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support. 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
The code is as follows:
require("class.phpmailer.php");
The code is 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"; var $Mailer = "smtp";
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
The code is as follows:
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.
If you need the Net_SMTP class, you can go to http://pear.php.net to download it. 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:
The code is as follows:
//---------------------------------------------
require_once 'Net/SMTP.php'; //Load class library
$subject = "Test Email";
$uid = $_POST['uid']; //from information
$content = "
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 address $smtp->rcptTo($u_email); //Recipient 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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
