如何将html内容添加到此Sendgrid的代码中?

时间:2017-03-25 10:46:19

标签: php email sendgrid

SendGrid-PHP库已有一些更新。请参阅自述文件中的示例:https://github.com/sendgrid/sendgrid-php



<?php
// If you are using Composer
require 'vendor/autoload.php';

// If you are not using Composer (recommended)
// require("path/to/sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "test@example.com");
$content = new SendGrid\Content("text/plain", "Hello, Email!");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();
&#13;
&#13;
&#13;

我想添加html内容,而不是纯文本,如何更改代码?

1 个答案:

答案 0 :(得分:1)

要发送html,这行:

$content = new SendGrid\Content("text/plain", "Hello, Email!");

必须如下:

$content = new SendGrid\Content("text/html", "Hello, Email!");
//                                    ^^^^

有关详情,请查看此example

更新

手动发送混合的html和纯文本电子邮件:

$boundary = uniqid('np');
$content = new SendGrid\Content("multipart/alternative;boundary=" . $boundary, "Hello, Email!");