Search code examples
phpemailcurlsendinblue

How to convert a curl to be used to send a mail in php


Im trying to use the following curl sample to send 2 mail to the user and the owner. If I want to send the mail with thefollowing code in php how will I be able to do it? Actually, the sample code is from a service called sendinblue. I would love to hear from you!

         curl -H 'api-key:your_access_key' -X POST -d '{"cc":["[email protected]":"cc whom!"],"text":"This is the text","bcc":["[email protected]":"bcc whom!"],"replyto":["[email protected]","reply to!"],"html":"This is the <h1>HTML</h1>This is inline image 1.<br/><img src=\"{myinlineimage1.png}\" alt=\"image1\" border=\"0\"><br/>Some text<br/>This is inline image 2.<br/><img src=\"{myinlineimage2.jpg}\" alt=\"image2\" border=\"0\"><br/>Some more text<br/>Re-used inline image 1.<br/><img src=\"{myinlineimage1.png}\" alt=\"image3\" border=\"0\">","to":{"[email protected]":"to whom!"},"attachment": {"myfilename.pdf":"your_pdf_files_base64_encoded_chunk_data"},"from":["[email protected]","from email!"],"subject":"My subject","headers":{"Content-Type":"text/html; charset=iso-8859-1", "X-param1":"value1","X-param2":"value2", "X-Mailin-custom":"my custom value","X-Mailin-IP":"102.102.1.2", "X-Mailin-Tag":"My tag"},"inline_image":{"myinlineimage1.png":"your_png_files_base64_encoded_chunk_data", "myinlineimage2.jpg":"your_jpg_files_base64_encoded_chunk_data"}}' 'https://api.sendinblue.com/v2.0/email

Want to send the following data

   $senddata = array (
               'to' => array('[email protected]'=>'[email protected]'),
               'from' => array($fromvalue,$fromvalue),
               'replyto' => array("[email protected]","[email protected]"),
                'subject' => "subject",
                'text' => "text",
                'html' => '',
                'fromname' => $fromnamevalue,
                'bcc' => 'bcc'
            );

Solution

  • You can convert curl command into php code with this website . https://incarnate.github.io/curl-to-php/ For you code here is the result.

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "https://api.sendinblue.com/v2.0/email");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"cc\":[\"[email protected]\":\"cc whom!\"],\"text\":\"This is the text\",\"bcc\":[\"[email protected]\":\"bcc whom!\"],\"replyto\":[\"[email protected]\",\"reply to!\"],\"html\":\"This is the <h1>HTML</h1>This is inline image 1.<br/><img src=\"{myinlineimage1.png}\" alt=\"image1\" border=\"0\"><br/>Some text<br/>This is inline image 2.<br/><img src=\"{myinlineimage2.jpg}\" alt=\"image2\" border=\"0\"><br/>Some more text<br/>Re-used inline image 1.<br/><img src=\"{myinlineimage1.png}\" alt=\"image3\" border=\"0\">\",\"to\":{\"[email protected]\":\"to whom!\"},\"attachment\": {\"myfilename.pdf\":\"your_pdf_files_base64_encoded_chunk_data\"},\"from\":[\"[email protected]\",\"from email!\"],\"subject\":\"My subject\",\"headers\":{\"Content-Type\":\"text/html; charset=iso-8859-1\", \"X-param1\":\"value1\",\"X-param2\":\"value2\", \"X-Mailin-custom\":\"my custom value\",\"X-Mailin-IP\":\"102.102.1.2\", \"X-Mailin-Tag\":\"My tag\"},\"inline_image\":{\"myinlineimage1.png\":\"your_png_files_base64_encoded_chunk_data\", \"myinlineimage2.jpg\":\"your_jpg_files_base64_encoded_chunk_data\"}}");
    curl_setopt($ch, CURLOPT_POST, 1);
    
    $headers = array();
    $headers[] = "Api-Key: your_access_key";
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    

    edited : just change this curl_setopt($ch, CURLOPT_POSTFIELDS , .... with this

    curl_setopt($ch, CURLOPT_POSTFIELDS,$senddata);