我尝试发送附件 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源代码,发现构造函数会验证name
和content
。
$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/