使用mailgun PHP API发送电子邮件

时间:2015-01-26 14:50:35

标签: php email guzzle mailgun

我正在尝试使用mailguns PHP api发送电子邮件:

define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');

$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

$mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@signstoptt.com',
                'to'        => $email,
                'subject'   => 'Sign Stop mailing list confirmation.',
                'html'      => "
                    Hello{$name},</br></br>
                    This is a test." 
            ]);

我甚至尝试使用array()而不是[]。

我的php错误日志中收到以下错误:

MissingRequiredParameters

这意味着我传递给post函数的内容不完整或不正确。在检查RestClient中的post函数时,我看到该函数需要2个数组而不是1,所以我尝试添加带有消息附件的第二个数组,它只是出现了更多错误,这次是guzzle(依赖于mailgun)

[26-Jan-2015 14:32:50 UTC] PHP Fatal error:  Uncaught exception 'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php:187
    Stack trace:
    #0 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php(116): Mailgun\Connection\RestClient->responseHandler(Object(Guzzle\Http\Message\Response))
    #1 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(106): Mailgun\Connection\RestClient->post('signstoptt.com/...', Array, Array)
    #2 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(53): Mailgun\Mailgun->post('signstoptt.com/...', Array, Array)
    #3 C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\subscribe.php(26): Mailgun\Mailgun->sendMessage('signstoptt.com', Array)
    #4 in C:\Users\Zachary\Documents\NetBeansProjects\SS_MailingList\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 187

是否还有其他人遇到此问题。我在netbeans设置的glassfish服务器上运行该站点。我还使用composer来安装mailgun及其依赖项。

编辑:添加了更多信息。

的init.php

<?php

    require_once 'vendor/autoload.php';

    define('MAILGUN_KEY', 'key-854743a7e');
    define('MAILGUN_PUBKEY', 'pubkey-b00e47d7');

    define('MAILGUN_DOMAIN', 'example.com');
    define('MAILGUN_LIST', 'customers@example.com');
    define('MAILGUN_SECRET','xjhbJH7');

    $mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

    $mailgunValidate = new Mailgun\Mailgun(MAILGUN_PUBKEY);

    $mailgunOptIn = $mailgun->OptInHandler();

subscribe.php

<?php

require_once 'init.php';

if(isset($_POST['name'], $_POST['email']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];

    $validate = $mailgunValidate->get('address/validate', [
            'address' => $email
        ])->http_response_body;

    if($validate->is_valid)
        {
            $hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email);

            $result = $mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@example.com',
                'to'        => $email,
                'subject'   => 'example mailing list confirmation.',
                'html'      => "
                    Hello{$name},</br></br>
                    You submitted a request to join our mailing list, to confirm this subscription please click on the link provided below.</br></br>
                    http://localhost:8000/confirm.php?hash={$hash}" 
            ]);


            $mailgun->post('lists/' . MAILGUN_LIST . '/members', [
                'name'          => $name,
                'address'       => $email,
                'subscribed'    => 'no'
            ]);

            header('Location: ./');

        }
}

?>

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Subscribe | Mailing list</title>
    </head>
    <body>
        <div class="container">
            <form action="subscribe.php" method="post">
                <div class="field">
                    <label>
                        Name
                        <input type="text" name="name" autocomplete="off">
                    </label>
                </div>
                <div class="field">
                    <label>
                        Email
                        <input type="text" name="email" autocomplete="off">
                    </label>
                </div>
                <input type="submit" value="Subscribe" class="button">
            </form>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:-1)

您忘记了邮件客户端无法使用text时使用的html密钥。

您的代码看起来像是

define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');

$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);

$mailgun->sendMessage(MAILGUN_DOMAIN, [
                'from'      => 'noreply@signstoptt.com',
                'to'        => $email,
                'subject'   => 'Sign Stop mailing list confirmation.',
                'text'      => 'Hello ' . $name . ', this is a test.',
                'html'      => '
                    Hello ' . $name . ',</br></br>
                    This is a test.'
            ]);
顺便说一下;为了便于阅读,我建议始终使用单引号或双引号。