【问题标题】:How to send out multi emails using mailgun API in PHP如何在 PHP 中使用 mailgun API 发送多封电子邮件
【发布时间】:2017-07-26 10:04:41
【问题描述】:

我已尝试运行以下代码以使用 PHP 中的 mailgun API 发送多封电子邮件。但是没有使用该代码发送电子邮件。我的代码是:

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);
 ?>
 #===============================================  

我在发送多封电子邮件的代码中是否有任何错误?如果您有任何解决方案,请提供帮助。

【问题讨论】:

  • 您可能不应该共享您的 API 密钥。我建议删除它。
  • 感谢您的建议

标签: php email mailgun


【解决方案1】:

我从他们的 API 中找到了这个。 (也许不是答案。只是一个参考)

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

【讨论】:

    【解决方案2】:

    除了 Abdulla 指出的内容之外,我看到您正在编写 "$domain" 作为文字域参数。如果你用引号写它,那么它将被解释为一个字符串,而不是变量本身。

    更改以下内容,它应该可以工作:

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

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

    【讨论】:

    • 在 php 中都应该工作。变量(以美元符号开头的字符串)在双引号内解析。它们不被解析为单引号。
    猜你喜欢
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多