【问题标题】:Send Email with SendGrid using Web Api php使用 Web Api php 使用 SendGrid 发送电子邮件
【发布时间】:2017-03-08 11:17:35
【问题描述】:

我在这里对 $pass 和 $api_key 感到困惑 它们是否相同,因为首先为 $pass 分配了 SendGrid 用户名的密码,但随后为 api_key 分配了 $pass。 如果它们相同,我们将在哪里使用我们在 SendGrid 上生成的 api_key? 请帮忙!!!

<?php 
$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD'; 

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

?>

【问题讨论】:

    标签: php api web sendgrid


    【解决方案1】:

    您尝试执行的操作存在一些问题:

    • 你永远不应该向sendgrid.com发送API调用,只能发送api.sendgrid.com
    • 您似乎正在尝试将 v2 API 与 Params 一起使用。由于您要设置一个新脚本,因此您应该使用v3 API,它使用正确的正文 JSON 参数,并且是 RESTful。
    • 您没有设置自己的脚本,而是查看了SendGrid PHP Library?
    • 对于脚本中的身份验证,您应该始终使用API Key,而不是您的主用户名和密码。

    看起来您有一个非常旧的示例脚本用于测试。你从哪里得到的?

    【讨论】:

    • 我不懂 PHP,也没有脚本。您是否查看过 SendGrid PHP 库?这应该会给你一个强有力的开端。
    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 2020-04-03
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多