dongposhi8677
2017-05-19 02:49
浏览 25
已采纳

如何转换curl用于在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":["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

Want to send the following data

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

图片转代码服务由CSDN问答提供 功能建议

我试图使用以下curl示例向用户和所有者发送2封邮件。 如果我想用php中的以下代码发送邮件,我将如何做到这一点? 实际上,示例代码来自名为sendinblue的服务。 我很乐意听取您的意见!

  curl -H'api-key:your_access_key'-X POST -d'{“cc”:[“cc@example.net  “:”cc who!“],”text“:”这是文本“,”bcc“:[”bcc@example.net“:”bcc who!“],”replyto“:[”replyto @ email。  com“,”回复!“],”html“:”这是&lt; h1&gt; HTML&lt; / h1&gt;这是内联图片1.&lt; br /&gt;&lt; img src = \“{myinlineimage1.png  } \“alt = \”image1 \“border = \”0 \“&gt;&lt; br /&gt;一些文字&lt; br /&gt;这是内联图片2.&lt; br /&gt;&lt; img src = \  “{myinlineimage2.jpg} \”alt = \“image2 \”border = \“0 \”&gt;&lt; br /&gt;更多文字&lt; br /&gt;重复使用内联图片1.&lt; br /&gt  ;&lt; img src = \“{myinlineimage1.png} \”alt = \“image3 \”border = \“0 \”&gt;“,”to“:{”to@example.net“:”向谁!  “},”attachment“:{”myfilename.pdf“:”your_pdf_files_base64_encoded_chunk_data“},”from“:[”from@email.com“,”来自电子邮件!“],”主题“:”我的主题“,”标题“  “:{”Content-Type“:”text / html; charset = iso-8859-1“,”X-param1“:”value1“,”X-param2“:”value2“,”X-Mailin-custom“  :“我的自定义值”,“X-Mailin-IP”:“102.102  .1.2“,”X-Mailin-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
nn

想要发送以下数据

  $ senddata  = array(
'到'=&gt;  array('sample_mail@live.com'=&gt;'sample_mail@live.com'),
'来自'=&gt; 数组($ fromvalue,$ fromvalue),
'replyto'=&gt;  array(“user_mail@live.com”,“user_mail@live.com”),
'subject'=&gt;  “subject”,
'text'=&gt;  “text”,
'html'=&gt;  '',
'fromname'=&gt;  $ fromnamevalue,
'bcc'=&gt;  'bcc'
); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulun1939 2017-05-19 03:40
    已采纳

    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\":[\"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);
    

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

    curl_setopt($ch, CURLOPT_POSTFIELDS,$senddata);
    
    打赏 评论
  • douzhaishan5462 2017-05-19 03:34

    You can download the Postman extension for Chrome. You get the option to view code for a request. In this case, you'll have code like below for your sample.

    $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;
    }
    
    打赏 评论

相关推荐 更多相似问题