问题描述
我正在尝试使用来自 sparkpost 的 api 从 netlify 函数发送电子邮件。但是当我使用 functions:invoke verify-email --no-identity 测试该功能时,我只收到 401 错误
0.7 * screen width
这是我得到的错误信息。我已经重新创建了 api-key 但结果相同
const SparkPost = require('sparkpost')
const client = new SparkPost(process.env.SPARKPOST)
const key = process.env.SPARKPOST
const handler = async(event,context,callback) => {
console.log(key) //outputs the key,so this is not the problem
return client.transmissions.send({
options: {
sandBox: true
},content: {
from: 'testing@sparkpostBox.com',subject: 'Hello,World!',html:'<html><body><p>Testing SparkPost - the world\'s most awesomest email service!</p></body></html>'
},recipients: [
{address: 'email@gmail.com'}
]
})
.then(data => {
console.log('Woohoo! You just sent your first mailing!');
console.log(data);
return data
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
return err
});
}
解决方法
这里可能存在两个问题之一。
-
确保您的 API 密钥在此处启用了传输读/写https://app.sparkpost.com/account/api-keys
-
您在欧盟并尝试使用美国服务器。在这种情况下,您可以执行以下操作:
'host' => 'api.eu.sparkpost.com',
或
$httpClient = new GuzzleAdapter(new Client()); $sparky = 新 SparkPost($httpClient,['key'=>'**********','host' => 'api.eu.sparkpost.com']);
在此处查看一些详细信息https://github.com/SparkPost/php-sparkpost/issues/180