2013-03-26 49 views
1

我需要在用户订阅完成时发送的电子邮件正文中添加链接。这个链接应该发送一封邮件到一个地址,该地址声明'用户名'想要续订他们的订阅。所以我需要做一个PHP邮件()内的PHP邮件(),如果你了解我!此外,他们点击链接后,他们应该得到一个谢谢你的消息。下面是当前'sendExpiryEmail'函数的php代码。希望你能帮助!谢谢。如何将PHP邮件()链接添加到自动“订阅过期”电子邮件的正文中?

公共函数sendExpiryEmail($电子邮件){

foreach($emails as $email){ 

     $name = $email['name']; 

     $emailaddress = $email['email']; 

     $expirydate = date('jS F Y',strtotime($email['datetime_expire'])); 



     $subject = "AZ China Report expiry reminder"; 



     $body = '<p><img src="http://az-china.com/images/azchina_logo_email.jpg"></p> 

       <p>Dear '.$name.',<br /><br /> 

       We hope you have been enjoying your subscription to the Black China Report.<br /><br /> 

       We aim to meet the needs of our readers, by de-mystifying the China market, and by providing accurate, current and pertinent facts and analysis.<br /> 

       We have some exciting new initiatives planned in the coming months.<br /><br /> 

       Your Black China Report subscription will expire on '.$expirydate.'.<br /><br /> 

       <strong>Renewing your subscription is easy.</strong><br /><br /> 

       Simply click here (link to mail()) and we will send you an order form and details on how to pay.<br /><br /> 

       If we can be any further assistance, please do not hesitate to contact us! <br /><br /> 

       Yours sincerely, <br /><br /> 

       Tom Martin<br /><br /> 

       AZ China</p>'; 



     // multiple recipients 

     $to = $emailaddress; 

     //$to = '[email protected]'; 



     // To send HTML mail, the Content-type header must be set 

     $headers = 'MIME-Version: 1.0' . "\r\n"; 

     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 



     // Additional headers 

     $headers .= 'From: AZ China <[email protected]>' . "\r\n"; 



     // Mail it 

     mail($to, $subject, $body, $headers); 

    } 

} 
+1

正文中的链接将转到您网站设置中的一个页面,以读取您在GET网址中提供的唯一ID,该ID将用于查找用户并执行他们发起的任何操作(然后删除那个唯一的ID,这样它就不会起作用,或者设置某种类型的状态='activated',或者其他)。关键是,url,也许'http://yoursite.com/renew.php?acct= [uniqid()]',将做发送,但需要由用户发起,并且url中的ID应该成为表格中唯一标识的行,用于解除用户帐户的引用并允许您从此处开始处理。 – 2013-03-26 07:07:21

+0

@JaredFarrish不会是一个答案 – eis 2013-03-26 07:13:19

+0

@ user2210482你明白你发送的邮件只是文本。阅读邮件的最终用户无论你写什么都不会奇迹般地拥有php mail()功能,所以你需要按照Jared Farrish解释的方式来完成。 – eis 2013-03-26 07:14:42

回答

0

来处理这一点的方法是设置一个查找或结合表,那里存储您每更新请求生成与相应的氨基酸唯一识别ID用户名。

该表可能看起来像:

renewals 
------------------------------- 
renewid    | userid 
------------------------------- 
[output of uniqid()] | bob 
[output of uniqid()] | bill 
[output of uniqid()] | sarah 
... 

当你运行你的脚本来收集账户即将到期清单,AA位添加到电子邮件的步骤是:

while ... 

    $renewid = uniqid(); 

    $renewurl = "http://.../renew.php?req=$renewid"; 

    // Put that URL in the body of the email. 

    $renewsql = " 
INSERT INTO renewals (
    renewid, userid 
) VALUES (
    '$renewid', '{$email['userid']}' 
) 
"; 

    if ($db->query($renewsql)) { 
     mail(...); 
    } 
} 

假设$email['userid']是用户ID字段被调用的任何内容,并且您在查询$emails中选择了它。

然后,像http://.../renew.php?req=$renewid一个网址,你可能会做renew.php是这样的:

<?php 

$renew = preg_replace('/^a-z0-9/i', '', $_GET['req']); 

if ($renew != '') { 
    $which = $db->query(" 
SELECT userid 
FROM renewals 
WHERE renewid = '$renew' 
LIMIT 1 
"); 

    if ($which->hasRows()) { 
     $userid = $which->get(0)->field('userid'); 

     // do whatever renewal stuff with the $userid 

     $db->query("DELETE FROM renewals WHERE renewid = '$renewid'"); 
    } 
} 

你可以做的是增加一个expires列,这样就可以定期清理旧续订请求另一件事。但或多或少,这是总体思路。

+0

谢谢这么多杰瑞德的详细答案。我会放弃它。所有其他的失败,我会去简单的mailto链接。对于后者,是否有邮件框中已经有模板消息?比如“我想续订我的订阅”, – tigerdi 2013-03-26 11:50:25

相关问题