使用PHPMailer发送邮件之后,如何知道对方是否阅读了邮件?
解决方法:
邮件内容可以以html的形式发送,可以在内容中插入图片,那么关键就在这个图片中了。
推荐阅读:
假设邮件内容如下,文件content.php代码:
 
复制代码 代码示例:
<table width="555" height="50" border="0" align="center" cellpadding="0" cellspacing="0" background="<?=$bg?>">
<tr>
 <td>这是测试内容</td>
   </tr>
   <tr>
 <td> </td>
   </tr>
   <tr>
 <td> </td>
   </tr>
 </table> 
 
这个$bg要怎么写呢?如果邮件是发送多个人呢?这里又怎么区分呢?
假设链接是:http://www.xxx.com/image.php?email=$email 再看看image.php的代码:
 
复制代码 代码示例:
<?php 
$email=$_GET["email"]; 
if($id) 
{ 
  
include_once("/conn.php"); 
  
mysql_query("update mail_list set has_read='yes' where email='$email'"); 
} 
header("Content/type:image/gif"); 
$im=imagecreatefromgif("bg.gif"); 
imagegif($im); 
imagedestroy($im); 
?>
所以,发送的邮件的内容为:
 
复制代码 代码示例:
<?php 
ob_start(); 
include_once("content.php"); 
$content=ob_get_contents(); 
ob_end_clean(); 
?>
写到这里,并没有对邮件内容中的背景图像进行替换。
以下将会逐步解决这个问题, 这个是用phpmail发送邮件的部分代码:
 
复制代码 代码示例:
<?php 
/* 
$contact=array("userA"=>"aaaa@sina.com","userB""=>"bbb@163.com","userC"=>"ccc@qq.com"); 
*/ 
foreach ($contact as $unm=>$email) 
{ 
  $bg_file="http://{$_SERVER['SERVER_NAME']}/image.php?email=$email"; 
  $str=str_replace('background=""',"background="$bg_file"",$content); 
  $mail->MsgHTML($str); 
  $mail->AddAddress($email,$unm); 
  $mail->Send(); 
} 
?>
这里为什么要用循环的方式逐一发送呢?因为为了区分是哪个联系人读过邮件,每封邮件的内容的背景图片的显示不一样。
在发送之前,对内容进行处理,对背景进行替换,这样看到的邮件内容的背景图像的src就变成了:http://www.domain.com/image.php?email=aaa@sina.com等等。
注意,执行上面的代码之后,会发现一个问题,发送到第一个人的是3封,第二个人的是2封,第三个人的是1封,这里为什么呢?
这里就要看看AddAddress方法了。
修改办法:
打开class.phpmailer.php,找到310行,将private $to=Array()改成public $to=Array().
然后,在发送邮件的代码中加入:
 
复制代码 代码示例:
$mail->to=Array();
$mail->AddAddress($email,$unm);
如此,就可以完成phpmailer发送邮件后,轻松获取别人是否阅读邮件的信息了。