使用Mailgun PHP API发送电子邮件

4

我正在尝试使用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个,因此我尝试添加第二个数组作为消息附件,但结果只是出现更多的错误,这次是与mailgun的依赖guzzle相关的错误。

[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>

你确定 $email 包含一个有效的电子邮件地址吗? - Dan Smith
我相当确定。我现在要硬编码以确保。编辑:我手动放了一个电子邮件,结果一样。就记录而言,信息来自一个表单,并由mailgun API验证,这是有效的。只是做其他事情似乎不行。 - matiszac
根据我所了解的情况,你看到的错误是因为Mailgun返回了一个400响应代码,这意味着他们没有得到他们期望的请求输入。我相当有信心这不是RestClient本身的错误。然而,需要有比我更多Mailgun经验的人来进一步帮助。很抱歉,我无法提供更多帮助。 - Dan Smith
@matiszac 你的代码是在闭包里还是直接在一个 PHP 类中?能否贴出它所包含的函数? - Chibueze Opata
@ChibuezeOpata 我已经编辑了我的帖子并添加了所有内容。告诉我你的想法。 - matiszac
2个回答

0

我之前遇到了类似的问题,但现在已经解决了。我们需要使用以下代码来通过Mailgun发送电子邮件 首先,您需要使用您的API密钥创建一个Mailgun类的实例:

$mg = Mailgun::create('YourAPIKEY'); // the key will be containing the work key in it (ie: key-123456789)

然后,使用创建的实例发送电子邮件:
$mg->messages()->send($domain, [
                'from'    => $from,
                'to'      => $email,
                'subject' => $subject,
                'sender' => $sender,
            'html' => $html
        ]);

上述代码适用于位于美国的域名。然而,如果您使用的是位于欧盟的Mailgun域名,则应将create函数修改如下:
$mg = Mailgun::create('YourAPIKEY','https://api.eu.mailgun.net');

随意根据您的喜好自定义这个答案版本。

-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.'
            ]);

顺便说一下,我建议始终使用单引号或双引号以提高可读性。

@mightyspaj 你有最新版本的Mailgun API(1.8)吗?这个版本应该会提供更好的错误描述。(如果你遇到了“传递给API的参数无效。请检查输入!”的问题) - Pepijn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接