search
HomeBackend DevelopmentPHP Tutorialphp多种形式发送邮件(mail qmail邮件系统 phpmailer类)_php实例

1. 使用 mail() 函数

没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。

2. 使用管道的形式

昨天刚测试成功,使用本地的qmail来发送邮件。

复制代码 代码如下:

/* 使用qmail发送邮件函数 */ 
function send_check_mail($email, $subject,$uid,$buffer) 

 $command =  "/var/qmail/bin/qmail-inject ".$email; //qmail程式地址,$email是要发送的地址 
 $handle = popen($command, "w"); //打开管道  http://www.cnblogs.com/roucheng/
 if (!$handle) { 
  return false; 
 }  

 $from = "webmaster@unixsky.net"; //发件人 
 fwrite($handle, "From: ".$from."\n"); //往管道写数据 
 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); //关闭管道 

 return true; 


------------------测试发送邮件: 

//发送邮件 

$subject = "测试邮件"; 

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

   ." 你好!

谢谢,本邮件测试!
"; //内容信息 

$u_email = "hren@yahoo.com.cn"; //发送到的邮箱 
if (send_check_mail($u_email, $subject, $uid, $content)) { 

 echo "恭喜!发送投票邮件到你的邮箱!

请检查你的邮箱:".$u_email."

". $close; 
 } else { 

 echo "非常不幸,发送投票邮件到你的邮箱失败,请重试或联系研发人员。

". $close; 

}

当然,也能使用相同的方法来处理sendmail的进程来发送邮件。

下面代码示例:

复制代码 代码如下:

$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"); 
?>

其实这种管道的方法比较底层,取决于你所调用程式的稳定性。所以是一种可选的发送邮件的方式。


3. 使用phpmailer类

是个开源的发送邮件类,主站:http://phpmailer.sourceforge.net

里面是两个文件,一个是class.smtp.php,更有以个是class.phpmailer.php
另外加上官方网站的使用方法:
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 " . $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. 使用PEAR::Net_SMTP组件

PEAR真是个好东西,可能非常多人都不怎么用,至少我目前使用他的DB类,发送邮件类都不错。

需要Net_SMTP类,能去 http://pear.php.net 下载,Net_SMTP类的使用手册:

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

我使用上面几个类,这个是最佳的,不管是速度还是别的,不过操作涉及到一些简单的smtp协议。

我的使用代码:

复制代码 代码如下:

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

require_once 'Net/SMTP.php'; //加载类库 

 
$subject = "测试邮件"; 

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

   ." 你好!

谢谢,本邮件测试!
"; //内容信息 

$u_email = "hren@yahoo.com.cn"; //发送到的邮箱 

$smtp = new Net_SMTP('192.168.0.1'); //smtp服务器 
$smtp->connect(); //连接服务器 
$smtp->helo('unixsky.net'); //发送HELO信息给服务器 
$smtp->mailFrom('hren@unixsky.net'); //发件人地址 
$smtp->rcptTo($u_email); //收件人地址 
$date = date('r'); //获取发信日期 
$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"); //添加发送数据并且发送 
$smtp->disconnect(); //关闭连接
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使用PHPMailer发送多人邮件的方法和步骤PHP使用PHPMailer发送多人邮件的方法和步骤May 22, 2023 pm 06:10 PM

在Web应用程序中,往往需要将邮件一次性发送给多个收件人。PHP是一种很流行的Web开发语言,而PHPMailer是一种常见的发送邮件的PHP类库。PHPMailer提供了丰富的接口,使得在PHP应用程序中发送邮件变得更加方便和易于使用。在本篇文章中,我们将介绍如何使用PHPMailer向多个收件人发送邮件的方法和步骤。下载PHPMailer首先需要在官网(

PHP开发实践:使用PHPMailer发送邮件到MySQL数据库中的用户PHP开发实践:使用PHPMailer发送邮件到MySQL数据库中的用户Aug 05, 2023 pm 06:21 PM

PHP开发实践:使用PHPMailer发送邮件到MySQL数据库中的用户引言:在现代互联网建设中,邮件是一种重要的沟通工具。无论是用户注册、密码重置,还是电子商务中的订单确认,发送电子邮件都是必不可少的功能。本文将介绍如何使用PHPMailer来发送电子邮件,并将邮件信息保存到MySQL数据库中的用户信息表中。一、安装PHPMailer库PHPMailer是

如何使用Flask-Mail发送电子邮件如何使用Flask-Mail发送电子邮件Aug 02, 2023 am 10:17 AM

如何使用Flask-Mail发送电子邮件随着互联网的发展,电子邮件已经成为了人们沟通的重要工具。在开发Web应用中,有时候我们需要在特定的场景下发送电子邮件,比如用户注册成功后发送欢迎邮件,或者用户忘记密码时发送重置密码邮件等。Flask是一款简单而又灵活的PythonWeb框架,而Flask-Mail是Flask框架下用于发送邮件的扩展库,本文将介绍如何

PHP使用PHPMailer库发送附件邮件的方法和注意事项PHP使用PHPMailer库发送附件邮件的方法和注意事项May 21, 2023 pm 06:12 PM

PHP使用PHPMailer库发送附件邮件的方法和注意事项邮件在现代生活中已经成为了非常重要的一种通信方式。在很多开发项目中,我们需要使用代码自动发送邮件,这时候PHPMailer库就是我们的不二之选。PHPMailer是一个专门用于PHP发送邮件的库。它可以方便地发送邮件,包括HTML格式的邮件和附件。本文将着重介绍PHPMailer库中如何发送带附件的邮

Python连接阿里云接口,实现邮件发送功能Python连接阿里云接口,实现邮件发送功能Jul 05, 2023 pm 04:33 PM

Python连接阿里云接口,实现邮件发送功能阿里云提供了一系列的服务接口,其中包括了邮件发送服务。通过Python脚本连接阿里云接口,我们可以实现邮件的快速发送。本篇文章将向您展示如何使用Python脚本连接阿里云接口,并实现邮件发送功能。首先,我们需要在阿里云上申请邮件发送服务,获取相应的接口信息。在阿里云管理控制台中,选择邮件推送服务,然后创建一个新的邮

如何使用PHP和PHPMAILER发送带有图片的HTML邮件?如何使用PHP和PHPMAILER发送带有图片的HTML邮件?Jul 21, 2023 am 09:21 AM

如何使用PHP和PHPMailer发送带有图片的HTML邮件?邮件在现代通信中扮演着重要的角色,但是发送带有图片的HTML邮件可能会让一些PHP开发者感到困惑。在本文中,我们将介绍如何使用PHP和PHPMailer来发送带有图片的HTML邮件。我们将提供代码示例来帮助您更好地理解如何实现这一目标。首先,我们需要确保PHPMailer库已经安装在我们的项目中

如何在CakePHP中使用PHPMailer?如何在CakePHP中使用PHPMailer?Jun 04, 2023 pm 01:10 PM

CakePHP是一个基于MVC模式的PHP开源框架,旨在为开发者提供高效、可扩展、易于维护的Web应用程序开发环境。其中,邮件功能一直是Web应用程序的重要组成部分之一。为了方便开发者使用邮件功能,在CakePHP中已经封装了PHPMailer类库。PHPMailer是一款常用的邮件发送类库,支持发送HTML邮件、附件、抄送、邮件队列和SMTP验证等功能。本

如何使用PHP队列发送邮件?如何使用PHP队列发送邮件?Sep 13, 2023 am 08:00 AM

如何使用PHP队列发送邮件?在现代的Web开发中,我们经常需要发送大量的电子邮件。无论是批量发送电子邮件给大量用户,还是根据用户行为发送个性化的电子邮件,使用队列来发送邮件是一个非常好的实践。队列可以帮助我们提高邮件发送的效率和稳定性,避免因为发送太多邮件而导致服务器负载过高,同时还可以处理发送失败的场景。在PHP开发中,我们可以使用常见的队列工具,如Rab

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool