【问题标题】:Create a Mailing List Subscribe Link with MailGun使用 MailGun 创建邮件列表订阅链接
【发布时间】:2014-07-20 15:19:11
【问题描述】:

我在一个私人的 php heroku 存储库上,想创建一个表单,允许我网站上的用户输入他们的电子邮件地址并提交表单并将电子邮件添加到 mailgun 上的现有邮件列表中。

我已经联系了 MailGun 支持,他们说可以并查看文档。

文档很模糊,我自己无法弄清楚。

是否有代码示例可以为我指明正确的方向?

【问题讨论】:

    标签: mailgun


    【解决方案1】:

    这是思路: * 第 1 步 - 创建一个包含您的电子邮件列表的邮件列表。 * 第 2 步 - 创建一个表单,询问用户他的电子邮件地址,要求他确认使用他的电子邮件,将电子邮件地址发送到给定地址以确认订阅。 *

    第 1 步

    # Include the Autoloader file (see documentation on how to install the php wrapper)
    require 'vendor/autoload.php';
    use Mailgun\Mailgun;
    
    # Instantiate the client. Use your APIKey
    $mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
    
    # Issue the call to the client.
    $result = $mgClient->post("lists", array(
        'address'     => 'my-user-list@mydomain.org',
        'description' => 'A List of users'
    ));
    

    这会创建一个名为“用户列表”的列表,并且可以在内部使用“my-user-list@mydomain.org”访问

    ** 第 2 步 **

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Widget</title>
    
    </head>
    <body>
        <form action="contact.php" method="POST" onsubmit="return check_the_box()">
            <input name="email" type="text" required="yes">
            <a href="#">I've read and agree to the ToS </a>
            <input id="chx" type="checkbox">
            <button type="submit" >Submit</button>
            <span style="display: none" id="pls"> Please check the Terms of Service box</span>
        </form>
        <script type="text/javascript">
        var box = document.getElementById("chx");
        var alrt = document.getElementById("pls");
        function check_the_box(){
            if(!box.checked) {
                alrt.setAttribute("style","display: inline");
                return false
    
            }
            else {
        return true
            }
    
        }
    </script>
    </body>
    </html>
    

    然后您需要从该表单中捕获字段并发送电子邮件:

    # Include the Autoloader     require 'vendor/autoload.php';
    use Mailgun\Mailgun;
    
    # Instantiate the client.
    $mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
    $domain = "mydomain.org";
    
    # Make the call to the client.
    $result = $mgClient->sendMessage($domain, array(
        'from'    => 'YOU@mydomain.org',
        'to'      => 'GUYWHOSIGNEDUP@hisdomain.com',
        'subject' => 'Confirmation Email',
        'html'    => 'Please, click this link to confirm you want to be part of my list <a href=\'http://website.com/confirmation.php?authcode=SOMEGENERATEDSTRING\'> CONFIRM </a>'
    ));
    

    为每个电子邮件地址创建一个生成的字符串

    用户点击链接后会被重定向并最终添加到邮件列表中:

    第 3 步

    添加到邮件列表

    # Include the Autoloader (see "Libraries" for install instructions)
    require 'vendor/autoload.php';
    use Mailgun\Mailgun;
    
    # Instantiate the client.
    $mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0l');
    $listAddress = 'my-user-list@mydomain.org';
    
    # Issue the call to the client.
    $result = $mgClient->post("lists/$listAddress/members", array(
        'address'     => 'GUYWHOSIGNEDUP@hisdomain.com',
        'description' => 'Signed up',
        'subscribed'  => true,
    ));
    

    完成!

    您需要做的就是创建每个用户唯一的生成代码,执行以下操作:

    function makeConfirmation($newguy) {
        $confirmCode = md5($newguy.'somestringonlyknowntome');
        return $confirmCode;
    }
    

    最后,在将他添加到邮件列表之前,我在他单击确认电子邮件时确认 URL 中发送到我的服务器的参数

    function checkConfirmation($conf,$email) {
        if($conf== md5($email.'somestringonlyknowntome')) {
            return true;
        } else { return false; }
    }
    

    相应地编辑..

    另外:

    Mailgun 的 github 存储库中的 PHP 有一个很好的教程供您学习。

    Tutorial for OPTins

    祝你好运,如果你成功了,请告诉我! :) 我很想收到您的来信,并在 mailgun 上写一篇关于此的博客!

    【讨论】:

    • 'to' => 'GUYWHOSIGNEDUP@hisdomain.com', 'html' => '请点击此链接确认您想加入我的列表 CONFIRM ' 'address' => 'GUYWHOSIGNEDUP@hisdomain.com', -- 如何为这些地址创建一个唯一字符串以使脚本可用?
    • 通过散列电子邮件地址与您只知道的盐字符串连接生成唯一字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多