【问题标题】:How to add the html content into this Sendgrid's code?如何将 html 内容添加到此 Sendgrid 的代码中?
【发布时间】:2017-03-25 10:46:19
【问题描述】:

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();

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

【问题讨论】:

    标签: php email sendgrid


    【解决方案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!");
    

    【讨论】:

    • 如果我更改为“text/html”,电子邮件将提示“消息只有 text/html MIME 部分”。所以,我想把html和纯文本加在一起,代码怎么写?
    • 虽然要做,还有很多工作要做,你可以联系包作者实现一个简单的方法,让你能够做到这一点;
    • @davidfnck 检查我更新的答案,我不确定这个尝试是否能正常工作,如果不能,你必须按照我说的联系包作者
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多