使用发送网格API键发送邮件 [英] Send mail using send grid api key

查看:147
本文介绍了使用发送网格API键发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用发送网格发送邮件。
这是我使用的脚本。

I'm using send grid for sending mails. This is the script I used.

$url = 'https://api.sendgrid.com/';
$params = array(
    'api_user'  => 'xxx',   // My send grid username
    'api_key'   => xxx',   // My send grid password
    'to'        => tomail,     
    'subject'   => 'sub',
    'html'      => 'message',
    'from'      => frommail,
);

$request =  $url.'api/mail.send.json';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);

工作正常,并成功发送邮件。

It's working fine and sending mails successfully.

我不使用发送网格API键发送邮件,而不使用密码。
我从' app.sendgrid.com/settings/api_keys 生成了api键ID和长键。

I wan't to use send grid api key for sending mails without using the password. I generated it from 'app.sendgrid.com/settings/api_keys' got api key id and long key.

如何使用来自web api调用的这些键。我用新生成的 api键名替换 api_user api_key $ c>和,但邮件未发送。

How can I use this keys from web api call. I'm replacing api_user and api_keys with newly generated api key name and key but mails are not sending.

推荐答案

今天也跑到这里。要添加到@bwest提供的答案:

I ran into this today as well. To add to the answer provided by @bwest:

$pass = 'your api token' // not the key, but the token

$url = 'https://api.sendgrid.com/';

//remove the user and password params - no longer needed
$params = array(
    'to'        => tomail,     
    'subject'   => 'sub',
    'html'      => 'message',
    'from'      => frommail,
);

$request =  $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$pass;

$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// add authorization header
curl_setopt($session, CURLOPT_HTTPHEADER,$headr);

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

这篇关于使用发送网格API键发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆