简体   繁体   English

如何在PHP中使用Mailgun API发送多封电子邮件

[英]How to send out multi emails using mailgun API in PHP

I have tried to run the following code for sending multiple emails using mailgun API in PHP . 我尝试运行以下代码,以使用PHP中的 mailgun API发送多封电子邮件。 But no email is sent using the code. 但是不会使用该代码发送电子邮件。 My code is : 我的代码是:

require 'vendor/autoload.php';
use Mailgun\Mailgun;

$mgClient = new Mailgun("my-api-key");
$domain = "sandboxfa3e9009746840be831c6edc9e9f1ee9.mailgun.org";
$result = $mgClient->sendMessage("$domain",
array('from' => 'Mailgun Sandbox - Test  
       <postmaster@sandboxfa3e9009746840be831c6edc9e9f1ee9.mailgun.org>',
    'to' => 'email_1@gmail.com, email_2@gmail.com',
    'subject' => 'Mailgun bulk-msg test for KB',
    'text' => 'Your mail do not support HTML',
    'html' => 'html-portion here...',
    'recipient-variables' => '{"email_1@gmail.com": {"first":"Name-1", 
     "id":1}, "email_2@gmail.com": {"first":"Name-2", "id": 2}}')
 );
 var_dump($result);
 ?>
 #===============================================  

Have i do any mistake in the code to send multi email ? 我在发送多封电子邮件的代码中是否犯了任何错误? If you have any solution, please help. 如果您有任何解决方案,请提供帮助。

I found this from their API. 我是从他们的API找到的。 (Not as answer maybe. Just a reference) (也许没有答案。仅供参考)

require 'vendor/autoload.php';
use Mailgun\Mailgun;

$mg = Mailgun::create('key-example');

# $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com',
  'to'      => 'sally@example.com',
  'subject' => 'The PHP SDK is awesome!',
  'text'    => 'It is so simple to send a message.'
]);

Source 资源

Aside from what Abdulla pointed out I see that you're writing "$domain" as the literal domain parameter. 除了Abdulla指出的内容外,我还看到您正在编写"$domain"作为文字域参数。 If you write it with quotation-marks, then it will be interpreted as a string, and not the variable itself. 如果用引号将其编写,则它将被解释为字符串,而不是变量本身。

Change the following and it should work: 更改以下内容,它应该可以工作:

$result = $mgClient->sendMessage("$domain",
[...]

to

$result = $mgClient->sendMessage($domain,
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM