发送蓝色附件

Sendinblue attachment

我尝试发送附件 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'=>'email@email.com'));
    $sendSmtpEmail['templateId'] = 39;
    $sendSmtpEmail['params'] = array(
    'NUMEROFACTURE'=> "12345",
    'CODECLIENT' => "1234567",
    'TOSEND' => "email1@email.net",
    '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,您有两种附加文件的方法,使用 urlcontent.

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 块数据。首先,获取服务器中的 pdf 路径为 $pdfdocPath。使用 file_get_contents 方法获取 pdf 内容并使用 base64_encode 方法对其进行编码。最后,使用 chunk_split 将内容分成小块,如下一个片段所示:

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail(); 
$sendSmtpEmail['to'] = array(array('email'=>'email@email.com'));
$sendSmtpEmail['templateId'] = 39;
$sendSmtpEmail['params'] = array(
'NUMEROFACTURE'=> "12345",
'CODECLIENT' => "1234567",
'TOSEND' => "email1@email.net",
'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'] = ['abc@example.com', 'asd@example.com'];
// 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;
}
    $dataEmail= new \SendinBlue\Client\Model\SendEmail();
    $dataEmail['emailTo']       = ['abc@example.com', 'asd@example.com'];
    $dataEmail['attachmentUrl'] = "http://www.ac-grenoble.fr/ia07/spip/IMG/pdf/tutoriel_pdf_creator-2.pdf";

    // if you want to use content attachment base64 
    // $b64Doc = chunk_split(base64_encode($data));
    // $attachment_array = array(array(
    //                              'content'=>$b64Doc,
    //                              'name'=>'Facture-'.$row["ClePiece"].'.pdf'
    //                          ));
    //  $dataEmail['attachment']    = $attachment_array;
    //Don't forget to delete attachmentUrl

    $templateId = 39;
    $dataEmail  = $dataEmail;

    $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;
    }

根据documentaitonSMTPApi->sendTransacEmail函数得到SendSmtpEmail对象。该对象对 attachment 属性有限制:

如果 templateId 通过且采用新模板语言格式,则仅接受附件 url。如果模板是旧模板语言格式,则忽略 attachment

但是SMTPApi->sendTemplate函数没有这个限制。

    $credentials = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR-KEY');

    $apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(new GuzzleHttp\Client(),$credentials);
        $sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
             'subject' => 'test email!',
             'sender' => ['name' => 'from name', 'email' => 'from@mail.com'],
             //'replyTo' => ['name' => 'test', 'email' => 'noreply@example.com'],
             'to' => [[ 'name' => 'Tushar Aher', 'email' => 'receivedto@gmail.com']],
             'htmlContent' => '<html><body><h1>This is a transactional email {{params.bodyMessage}}</h1></body></html>',
             'params' => ['bodyMessage' => 'this is a test!']
        ]);

    /*$attachement = new \SendinBlue\Client\Model\SendSmtpEmailAttachment();
    $attachement['url']= FCPATH.'uploads/invoice/ticket-498410.pdf';
    $attachement['name']= 'ticket-498410.pdf';
    $attachement['content']= "utf-8";
    $sendSmtpEmail['attachment']= $attachement;*/

    // PDF wrapper

    $pdfDocPath = FCPATH.'uploads/invoice/ticket-498410.pdf';
    $content = chunk_split(base64_encode(file_get_contents($pdfDocPath)));
    // Ends pdf wrapper
    $attachment_item = array(
            'name'=>'ticket-498410.pdf',
            'content'=>$content
    );
    $attachment_list = array($attachment_item);
    // Ends pdf wrapper

    $sendSmtpEmail['attachment']    = $attachment_list;


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