【问题标题】:Sendgrid will only send utf-8 attachmentsSendgrid 只会发送 utf-8 附件
【发布时间】:2013-07-16 12:47:39
【问题描述】:

我创建了一个就业申请,允许申请人上传 pdf、doc 或 docx 扩展名的简历。然后,我使用 Sendgrid 将他们的信息和简历通过电子邮件发送给人力资源部。

我通过测试发现发送邮件时收到错误:

参数附件[resume.pdf]不是utf8

我该如何解决这个问题,我是否应该在将上传到电子邮件的每个文件都编码为 utf-8 之前将其附加到电子邮件中?这会产生任何问题或单独修改用户上传的简历吗?

这是我用来通过 SendGrid API 发送的 PHP Curl 代码: (注意:我必须使用 REST API,客户端 Web 服务器上没有配置 SMTP)

    <?php

        $mail['from'] = 'humanresources@email.org';
                    $mail['fromname'] = 'Human Resources';
                    $mail['to'] = 'person@email.com';



                    $mail['subject'] = character_limiter('Employment: '. $application['position'], 50);

                    $mail['html'] = '<p><strong>Name:</strong> '.$application['firstname'].' '.$application['lastname'].'</p>';
                    $mail['html'] .= '<p><strong>Position:</strong> '.$application['position'].'</p>';
                    $mail['html'] .= '<p><strong>Date:</strong> '.mdate('%m/%d/%Y %g:%i %A', $application['timestamp_saved']).'</p>';
                    $mail['html'] .= '<p><strong>Email:</strong> '.$application['email'].'</p>';


                    $mail['files['.$application['pdf'].']'] = '@saved_applications/'. $application['pdf'];



                    //Sendgrid Credientals
                    $mail['api_user']  = 'sendgrid_user';
                    $mail['api_key']   = 'sendgrid_pass';

                    print_r($mail);

                    // Generate curl request
                    $session = curl_init('https://sendgrid.com/api/mail.send.json');
                    curl_setopt ($session, CURLOPT_POST, true);
                    curl_setopt ($session, CURLOPT_POSTFIELDS, $mail);
                    curl_setopt($session, CURLOPT_HEADER, false);
                    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

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

                    // print everything out
                    $output = json_decode($response, TRUE);

                    print_r($output);

?>

【问题讨论】:

  • sendgrid 端好像有个bug,你试过他们的支持吗?

标签: php utf-8 encode sendgrid


【解决方案1】:

您拥有的代码应该可以工作(并且在我测试时可以工作)。

您可能想要使用PHP library,而不是平面 cURL。您可以通过执行以下操作从 Web Web API send attachments

<?php
    $sendgrid = new SendGrid('username', 'password');
    $mail = new SendGrid\Mail();
    $mail->
    addTo('humanresources@email.org')->
    ...
    addAttachment('saved_applications/'. $application['pdf']);
    $sendgrid->web->send($mail);
?>

【讨论】:

    【解决方案2】:

    也许不使用 sendgrid 的 Web 服务 API 来发送这些消息,只需使用 phpmailer 并使用他们的外发邮件服务器 smtp.sendgrid.net 通过 sendgrid 通过 SMTP 发送消息。如果您以这种方式发送消息,我相信您不会收到此错误。请参阅https://github.com/PHPMailer/PHPMailer 的示例。

    【讨论】:

    • 使用 SMTP 是我最初想采取的路线。但是客户端使用的托管服务提供商没有在他们的网络服务器上配置 SMTP :(
    • 使用 phpmailer,您不需要在主机上运行 SMTP 服务器。它将使用远程 SMTP 服务器发送消息,例如 Sendgrid 的 smtp.sendgrid.net。
    • 而且它易于使用,易于设置。只需将 3 个文件复制到您的托管帐户(包含在您的 PHP 脚本中),然后使用我上面复制的链接中的示例作为发送消息(带附件)的样板。
    猜你喜欢
    • 2016-08-05
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多