Как конвертировать curl для отправки почты в php

Я пытаюсь использовать следующий пример скручивания, чтобы отправить 2 письма пользователю и владельцу. Если я хочу отправить письмо с следующим кодом в php, как я смогу это сделать? На самом деле, пример кода взят из сервиса под названием sendinblue. Я хотел бы услышать от вас!

         curl -H 'api-key:your_access_key' -X POST -d '{"cc":["cc@example.net":"cc whom!"],"text":"This is the text","bcc":["bcc@example.net":"bcc whom!"],"replyto":["replyto@email.com","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":{"to@example.net":"to whom!"},"attachment": {"myfilename.pdf":"your_pdf_files_base64_encoded_chunk_data"},"from":["from@email.com","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

Хочу отправить следующие данные

   $senddata = array (
               'to' => array('sample_mail@live.com'=>'sample_mail@live.com'),
               'from' => array($fromvalue,$fromvalue),
               'replyto' => array("user_mail@live.com","user_mail@live.com"),
                'subject' => "subject",
                'text' => "text",
                'html' => '',
                'fromname' => $fromnamevalue,
                'bcc' => 'bcc'
            );

2 ответа

Решение

Вы можете конвертировать команду curl в php-код на этом сайте. https://incarnate.github.io/curl-to-php/ Для вас код здесь - результат.

$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\":[\"cc@example.net\":\"cc whom!\"],\"text\":\"This is the text\",\"bcc\":[\"bcc@example.net\":\"bcc whom!\"],\"replyto\":[\"replyto@email.com\",\"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\":{\"to@example.net\":\"to whom!\"},\"attachment\": {\"myfilename.pdf\":\"your_pdf_files_base64_encoded_chunk_data\"},\"from\":[\"from@email.com\",\"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);

отредактировано: просто измените этот curl_setopt($ch, CURLOPT_POSTFIELDS, .... с этим

curl_setopt($ch, CURLOPT_POSTFIELDS,$senddata);

Вы можете скачать расширение Postman для Chrome. Вы получаете возможность просмотра кода для запроса. В этом случае у вас будет код, как показано ниже для вашего примера.

$request = new HttpRequest();
$request->setUrl('https://api.sendinblue.com/v2.0/email');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'api-key' => 'your_access_key',
));

$request->setBody('{
  "cc":["cc@example.net":"cc whom!"],
  "text":"This is the text",
  "bcc":["bcc@example.net":"bcc whom!"],
  "replyto":["replyto@email.com","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":{"to@example.net":"to whom!"},
  "attachment": {
    "myfilename.pdf":"your_pdf_files_base64_encoded_chunk_data"
  },
  "from":["from@email.com","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"
  }
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}
Другие вопросы по тегам