[英]How to save emails in an email list on SendGrid using Laravel?
我正在开发一项功能,一旦用户注册了一个帐户,他将在一段时间内开始收到一系列 email(电子邮件滴)。
我正在使用 Laravel。我需要在注册时获取 email并将其保存在 SendGrid 上的 email 列表中。 但我没有在sendgrid/sendgrid-php
包的文档中找到它。
那么,您能告诉我如何实现吗?
可以使用 sendgrid-php package 来完成:
use SendGrid;
$api = new SendGrid(YOUR_API_KEY);
$contacts = [
[
'email' => "email@something",
'address_line_1' => '',
'city' => '',
'country' => '',
'first_name' => '',
'last_name' => '',
etc....
]
]
$request_body = [
'contacts' => $contacts
];
$response = $api->client->marketing()->contacts()->put($request_body);
if($response->statusCode() !== 202) {
// error has occured
}
您可以为此使用 SendGrid PHP package。 以下是如何执行此操作的示例:
在 Laravel 中注册 controller:
use SendGrid;
use SendGrid\Mail\To;
class SendGridController extends Controller
{
public function register(Request $request)
{
// ... your rego code
$apiKey = 'YOUR_API_KEY';
$sg = new SendGrid($apiKey);
$list_id = 'YOUR_LIST_ID';
//Add an email to the list
$email = new To();
$email->setEmail($request->input('email'));
$email->setFirstName($request->input('first_name'));
$email->setLastName($request->input('last_name'));
$response = $sg->client->marketing()->lists()->recipients()->post($list_id, $email->jsonSerialize());
return $response->statusCode();
}
}
您还可以参考SendGrid API 文档,了解有关如何创建、更新和删除列表以及添加和删除收件人的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.