【问题标题】:How to add a php mail() link to the body of an automated 'subscription expired' email?如何将 php mail() 链接添加到自动“订阅过期”电子邮件的正文中?
【发布时间】:2013-03-26 15:48:07
【问题描述】:

我需要在用户订阅完成时发送的电子邮件正文中添加一个链接。然后,此链接应向一个地址发送一封电子邮件,该地址表明“用户名”想要续订他们的订阅。所以我需要在 php mail() 里面做一个 php mail(),如果你理解我的话!此外,在他们单击链接后,他们应该会收到一条感谢消息。当前 'sendExpiryEmail' 函数的 php 代码如下。希望你能帮忙!谢谢。

公共函数 sendExpiryEmail($emails){

    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 = 'c23gooey@gmail.com';



        // 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 <tom.martin@az-china.com>' . "\r\n";



        // Mail it

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

    }

}

【问题讨论】:

  • 正文中的链接将转到您网站设置上的页面,以读取您在 GET url 中提供的唯一 ID,该 ID 将用于查找用户并执行他们的任何操作已启动(然后删除该唯一 ID,使其不可操作,或设置某种类型的状态 = activated,或其他)。关键是 url,可能是http://yoursite.com/renew.php?acct=[uniqid()],将进行发送,但需要由用户发起,并且 url 中的 ID 应该是表中的唯一标识行,该行取消引用用户的帐户并允许您从那里开始工作。
  • @JaredFarrish 这不是一个答案
  • @user2210482 你明白你发送的邮件只是文本。无论您写什么,阅读您邮件的最终用户都不会神奇地拥有 php mail() 功能,因此您需要按照 Jared Farrish 解释的方式进行操作。
  • @eis,我添加了一些东西。它是伪代码,但可以理解一般概念。
  • 感谢您的帮助。只是想确认 jareds 的答案是做这样的事情的唯一方法吗?如果是这样,您介意慢慢解释这些步骤吗?如您所知,我对此比较陌生。

标签: php


【解决方案1】:

解决此问题的一种方法是设置一个查找表或联结表,您将在其中存储一个唯一标识 ID,该 ID 是您为每个续订请求生成的具有相应用户 ID 的。

此表可能类似于:

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

当您运行脚本来收集即将到期的帐户列表时,请在电子邮件步骤中添加一些内容:

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 这样的 URL,您可能会在 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 列,以便您可以定期清除旧的续订请求。但或多或少,这是一般的想法。

【讨论】:

  • 非常感谢 Jared 的详细回答。我会试一试。一切都失败了,我会去简单的mailto链接。对于后者,邮箱中是否已经有模板消息?例如“我想续订_____”
猜你喜欢
  • 2018-08-28
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2020-07-28
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多