我正在尝试使用 mailguns PHP api 发送电子邮件:
define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => '[email protected]',
'to' => $email,
'subject' => 'Sign Stop mailing list confirmation.',
'html' => "
Hello{$name},</br></br>
This is a test."
]);
我什至尝试使用 array() 而不是 [ ]。
我在 php 错误日志中收到以下错误:
缺少必需参数
这意味着我传递给 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', '[email protected]');
define('MAILGUN_SECRET','xjhbJH7');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgunValidate = new Mailgun\Mailgun(MAILGUN_PUBKEY);
$mailgunOptIn = $mailgun->OptInHandler();
订阅.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' => '[email protected]',
'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>
我遇到了类似的问题,但现在已经解决了。我们需要以下代码从 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 域,则应按如下方式修改创建函数:
$mg = Mailgun::create('YourAPIKEY','https://api.eu.mailgun.net');
您可以根据您的喜好随意定制此版本的答案。
您忘记了
text
键,当邮件客户端无法使用 html
时,将使用该键。define('MAILGUN_KEY', 'key-ExamPle3xAMPle');
define('MAILGUN_DOMAIN', 'example.com');
$mailgun = new Mailgun\Mailgun(MAILGUN_KEY);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => '[email protected]',
'to' => $email,
'subject' => 'Sign Stop mailing list confirmation.',
'text' => 'Hello ' . $name . ', this is a test.',
'html' => '
Hello ' . $name . ',</br></br>
This is a test.'
]);
顺便说一句;为了便于阅读,我建议始终使用单引号或双引号。