search
HomeBackend DevelopmentPHP TutorialSend 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 = "

".$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 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/727575.htmlTechArticleThis article mainly introduces various methods of sending emails in PHP, including using the mail() function and using pipes Form, use phpmailer class and other methods, please refer to it 1. Use m...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),