【问题标题】:Sendinblue attachmentSendinblue 附件
【发布时间】:2019-01-24 06:52:08
【问题描述】:

我尝试发送附件 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;
}

【问题讨论】:

  • $attachement['url'] 必须是附件的绝对 URL(NO 是本地文件)。检查 [SendInBlue 文档][github.com/sendinblue/APIv3-php-library/blob/master/docs/Model/…
  • 如果文件在私有服务器中,会不会是问题所在?例如:192.168.10.01:91/facture/…'
  • 请阅读文档,您有两种方法可以通过urlcontent 附加文件。请记住,内容是动态生成的附件的 Base64 编码块数据。您错误地将 "utf-8" 分配给内容。这意味着您需要将 pdf 数据转换为 base64 块数据。 $attachment_content = chunk_split(base64_encode($contentPdf)); 像这样。

标签: php email-attachments sendinblue


【解决方案1】:

根据the SendSmtpEmailAttachment documentation,您有两种方法可以使用urlcontent 附加文件。

网址 |附件的绝对url(没有本地文件)。

内容 |动态生成的附件的 Base64 编码块数据

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

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

【讨论】:

  • 我已经尝试过了,但它不起作用。我不知道问题是否出在 \SendinBlue\Client\Api\SMTPApi() 方法 sendTransacEmail 中。当我尝试在这里发送附件时:developers.sendinblue.com/v3.0/reference#sendtransacemail 与 sendtransacEmail 它不起作用。
  • 它只是不工作或者你有一个错误?如果您遇到错误,请告诉我。
  • 我没有错误,我收到了电子邮件但没有附件。我想是的,这是api错误。我尝试使用 sendinblue Api 在线测试 sendtransacEmail() 功能:developers.sendinblue.com/v3.0/reference#sendtransacemail 我没有收到附件。但是当我尝试使用:developers.sendinblue.com/v3.0/reference#sendtemplate-1 时,它会向我发送一个附件('url 附件')。当我使用 content (base64) 时,它不起作用。我不知道为什么,我没有收到任何错误。现在,我将代码更改为使用带有 url 附件的 sendTemplate()。
  • 当我使用 url 附件时它现在正在工作。如果它也有效,我必须尝试内容附件。
  • 试试这个新的更新。我找到了如何验证内容。
【解决方案2】:
    $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;
    }

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:
          $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;
          }
      

      【讨论】:

        猜你喜欢
        • 2022-10-04
        • 2022-07-15
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多