php - 发送蓝色附件

标签 php email-attachments sendinblue

我尝试发送附件 pdf 文件。我收到电子邮件但没有附件。 我尝试使用 https://github.com/sendinblue/APIv3-php-library/blob/master/docs/Model/SendSmtpEmail.mdenter

 $sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail(); 
    $sendSmtpEmail['to'] = array(array('email'=>'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47222a262e2b07222a262e2b6924282a" rel="noreferrer noopener nofollow">[email protected]</a>'));
    $sendSmtpEmail['templateId'] = 39;
    $sendSmtpEmail['params'] = array(
    'NUMEROFACTURE'=> "12345",
    'CODECLIENT' => "1234567",
    'TOSEND' => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741119151d1845341119151d185a1a1100" rel="noreferrer noopener nofollow">[email protected]</a>",
    'MONTANTFACTURE'=>  number_format(12, 2, ',', ' '),
    );
    $attachement = new \SendinBlue\Client\Model\SendSmtpEmailAttachment();
    $attachement['url']= __DIR__'/facture/Facture-'.$row["ClePiece"].'.pdf';
    $attachement['name']= 'Facture-'.$row["ClePiece"].'.pdf';
    $attachement['content']= "utf-8";
    $sendSmtpEmail['attachment']= $attachement;
    $sendSmtpEmail['headers'] = array('Content-Type'=>'application/pdf','Content-Disposition'=>'attachment','filename'=>'Facture-'.$row["ClePiece"].'.pdf',"charset"=>"utf-8");


$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR_API_KEY');
$apiInstance = new SendinBlue\Client\Api\SMTPApi(new GuzzleHttp\Client(),$config);

try {
    $result = $apiInstance->sendTransacEmail($sendSmtpEmail);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling SMTPApi->sendTransacEmail: ', $e->getMessage(), PHP_EOL;
}

最佳答案

根据 the SendSmtpEmailAttachment documentation ,您有两种方法使用 url内容 附加文件。

url | Absolute url of the attachment (no local file).

content | Base64 encoded chunk data of the attachment generated on the fly

您错误地将“utf-8”分配给内容。这意味着您需要将 pdf 数据转换为 base64 block 数据。首先,获取服务器中的 pdf 路径作为 $pdfdocPath。使用file_get_contents方法获取pdf内容并使用base64_encode方法对其进行编码。最后,使用 chunk_split 将内容分割成小块,如下一个代码片段所示:

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail(); 
$sendSmtpEmail['to'] = array(array('email'=>'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c1c9c5cdc8e4c1c9c5cdc88ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>'));
$sendSmtpEmail['templateId'] = 39;
$sendSmtpEmail['params'] = array(
'NUMEROFACTURE'=> "12345",
'CODECLIENT' => "1234567",
'TOSEND' => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="355058545c5904755058545c591b5b5041" rel="noreferrer noopener nofollow">[email protected]</a>",
'MONTANTFACTURE'=>  number_format(12, 2, ',', ' '),
);
$pdfdocPath = __DIR__.'/facture/Facture-'.$row["ClePiece"].'.pdf';
$b64Doc = chunk_split(base64_encode(file_get_contents($pdfdocPath)));
$attachement = new \SendinBlue\Client\Model\SendSmtpEmailAttachment();
$attachement['name']= 'Facture-'.$row["ClePiece"].'.pdf';
$attachement['content']= $b64Doc;
$sendSmtpEmail['attachment']= $attachement;
$sendSmtpEmail['headers'] = array('Content-Type'=>'application/pdf','Content-Disposition'=>'attachment','filename'=>'Facture-'.$row["ClePiece"].'.pdf',"charset"=>"utf-8");

更新:

我检查了APIv3-php-library源代码,发现构造函数会验证namecontent

$dataEmail = new \SendinBlue\Client\Model\SendEmail();
$dataEmail['emailTo'] = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b1b2b390b5a8b1bda0bcb5feb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfaebcab8faab7aea2bfa3aae1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>'];
// PDF wrapper
$pdfDocPath = __DIR__.'/facture/Facture-'.$row["ClePiece"].'.pdf';
$content = chunk_split(base64_encode(file_get_contents($pdfDocPath)));
// Ends pdf wrapper
$attachment_item = array(
        'name'=>'Facture-'.$row["ClePiece"].'.pdf',
        'content'=>$content
);
$attachment_list = array($attachment_item);
// Ends pdf wrapper

$dataEmail['attachment']    = $attachment_list;

$templateId = 39;

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR_API_KEY');

$apiInstance = new SendinBlue\Client\Api\SMTPApi(new GuzzleHttp\Client(),$config);
try {
    $result = $apiInstance->sendTemplate($templateId, $dataEmail);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling SMTPApi->sendTemplate: ', $e->getMessage(), PHP_EOL;
}

关于php - 发送蓝色附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340866/

相关文章:

php - 在 PHP 中使用 cURL 的 SSL Twitter oauth

php - 开发消息服务器的最佳结构

php - 避免php中http_build_query中HTML实体的转换

电子邮件附件加密 - RSA 还是 AES?

javascript - 使用 Google Apps 脚本设置 Sendinblue 邮件服务

php - 将字符串变量插入数组

javascript - 表中没有可用数据,但显示了数据库中的数据

python - 压缩附件中断 email.message.Message.get_payload()

java - 发送蓝色 : sending mail with attachement to sendinblue

android - 附加的 apk 文件保持未更新