2

I try to send attachment pdf file. I get the email but no attachmetn. I have try to use 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;
}
Elierak26
  • 38
  • 1
  • 1
  • 6
  • `$attachement['url']` must be an absolute url of the attachment (**NO** a local file). Check the [SendInBlue Documentation][https://github.com/sendinblue/APIv3-php-library/blob/master/docs/Model/SendSmtpEmailAttachment.md] – Teocci Jan 24 '19 at 07:03
  • If the file is in a private server, can that be the problem? for example: http://192.168.10.01:91/facture/Facture-'.$row["ClePiece"].'.pdf' – Elierak26 Jan 24 '19 at 07:14
  • Please, read the documentation, you have two ways to attach a file by `url` or by `content`. Remember that the content is a *Base64 encoded chunk data of the attachment generated on the fly*. You are wrongly assigning `"utf-8"` to the content. This mean you need to convert the pdf data into a base64 chunk data. `$attachment_content = chunk_split(base64_encode($contentPdf));` like this. – Teocci Jan 24 '19 at 07:19

4 Answers4

3

According to the SendSmtpEmailAttachment documentation, you have two ways to attach a file using a url or a content.

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

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

You are wrongly assigning "utf-8" to the content. This mean you need to convert the pdf data into a base64 chunk data. First, get the pdf path in your server as $pdfdocPath. Get the pdf content using file_get_contents method and encode it using base64_encode method. Finally, split the content in small chunks using chunk_split as shown in the next snippet:

$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");

Update:

I checked the APIv3-php-library source code and I found that the constructor will do the validation of name and content.

$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;
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
  • I have try this but it's not working. I don't know if the problem is in the \SendinBlue\Client\Api\SMTPApi() Method sendTransacEmail. When I try to send attachement here : https://developers.sendinblue.com/v3.0/reference#sendtransacemail with sendtransacEmail it's not working. – Elierak26 Jan 24 '19 at 13:26
  • It is jut not working or you got an error? If you get an error let me know. – Teocci Jan 24 '19 at 22:11
  • i don't have error, i get the email but no attachment. I think so, it's api bug. I have try to use sendinblue Api online to test sendtransacEmail() function : https://developers.sendinblue.com/v3.0/reference#sendtransacemail i don't get the attachement. But when I try to use : https://developers.sendinblue.com/v3.0/reference#sendtemplate-1 , that send me an attachement ( 'url attachment'). When i use content (base64) , it's not working. I don't know why, i dont get any error. Now, i change my code to use sendTemplate() with url attachment. – Elierak26 Jan 25 '19 at 06:25
  • It's working now when i use url attachment. I have to try content attachment if it's working too. – Elierak26 Jan 25 '19 at 06:50
  • Try this new update. I found how to validate the content. – Teocci Jan 25 '19 at 07:10
  • I have send email to sendinblue contact about this problem and when they replay me, i will tell you what the problem. They tell me, no error un the code, but they don't know why that's not working – Elierak26 Jan 25 '19 at 17:31
  • Then could you please mark my answer as the answer because it shows how to attach a pdf file using `content` by converting the pdf in base64 and slicing it in small chunks properly. Thank you – Teocci Jan 25 '19 at 23:44
  • That's answer are not valid now because the APi have some problem. The send me a response about sendTemplate() to use content. like this $b64Doc = chunk_split(base64_encode($data)); $attachment_array = array('content'=>$b64Doc,'name'=>'Facture'.$row["ClePiece"].'.pdf'); $dataEmail['attachment'] = array($attachment_array); – Elierak26 Jan 28 '19 at 14:43
  • This approach also works for a ruby lib - `APIv3-ruby-library` – Roman Malkevych Jan 29 '20 at 17:59
0
    $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;
    }
Elierak26
  • 38
  • 1
  • 1
  • 6
0

According to the documentaiton SMTPApi->sendTransacEmail function gets SendSmtpEmail object. That object has restrictions for the attachment attribute:

If templateId is passed and is in New Template Language format then only attachment url is accepted. If template is in Old template Language format, then attachment is ignored.

But SMTPApi->sendTemplate function don't have this restriction.

Roman Malkevych
  • 260
  • 1
  • 4
  • 11
0
    $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;
    }