使用PHPMailer发送邮件

十一 5th, 2008

PHPMailer是一个用于发送电子邮件的PHP函数包。
官方网址 http://phpmailer.codeworxtech.com/index.php
下载网址 http://sourceforge.net/project/showfiles.php?group_id=26031

它提供的功能包括:

  • 在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
  • 支持多种邮件编码包括:8bit,base64,binary和quoted-printable
  • 支持SMTP验证
  • 支持冗余SMTP服务器
  • 支持带附件的邮件和Html格式的邮件
  • 自定义邮件头
  • 支持在邮件中嵌入图片
  • 调试灵活
  • 经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等
  • 可运行在任何平台之上

例子代码 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//使用MySQL数据库的例子
require("class.phpmailer.php");  
$mail = new PHPMailer();  
$mail->From     = "list@mydomain.com";  //发件人邮件地址   
$mail->FromName = "List manager";  //发件人姓名
$mail->Host     = "smtp1.site.com;smtp2.site.com"; //smtp主机.可多个使用;(分号)分开   
$mail->Mailer   = "smtp";  //使用smtp
 
//例子查询数据库  
@MYSQL_CONNECT("localhost","root","password");   
@mysql_select_db("my_company");   
$query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";   
$result = @MYSQL_QUERY($query);  
while ($row = mysql_fetch_array ($result)) {   
    // HTML body 
    // HTML内容 
    $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();//清除附件
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require("class.phpmailer.php");   
//扩展自己的mail类  
class MyMailer extends PHPMailer {   
    // Set default variables for all new objects
  //设定默认值   
   var $From     = "from@email.com";   
    var $FromName = "Mailer";   
    var $Host     = "smtp1.site.com;smtp2.site.com";   
    // Alternative to IsSMTP()   是否使用smtp
    var $Mailer   = "smtp"; 
    var $WordWrap = 50;
    var $CharSet = 'utf-8'; //编码
   var $Encoding = 'base64';
 
    // 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   
    }   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 使用自己的推展类,发送附件邮件
require("mail.inc.php");   
// Instantiate your new class 
//使用自己的扩展类  
$mail = new MyMailer;     
// Now you only need to add the necessary stuff   
$mail->AddAddress("josh@site.com", "Josh Adams");   
$mail->Subject = "Here is the subject";   
$mail->Body    = "This is the message body";   
//添加附件
//$path 文件位置
//$filename 显示的附件名称(可选)
$mail->AddAttachment($path = "c:/temp/11-10-00.zip", $filename= "new_name.zip");  // optional name   
 
if(!$mail->Send())   
{   
   echo "There was an error sending the message";   
   exit;   
}   
 
echo "Message was sent successfully";
标签: ,
  1. Casino 1249768105
    八 9th, 200910:50
    #1