phpmailer發送郵件之後,返回收件者是否閱讀了郵件的方法_php技巧

來源:互聯網
上載者:User

很多人在使用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的代碼

檔案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);

寫到這裡,就不用再做過多說明了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.