2017-05-19 47 views
0

我試圖使用以下curl示例發送2郵件給用戶和所有者。如果我想在php中使用以下代碼發送郵件,我將如何做到這一點?實際上,示例代碼來自名爲sendinblue的服務。我很想聽到你的消息!如何轉換curl用於發送郵件在php

  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 

想送以下數據

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

在(因此)我們希望你嘗試**自己編寫的代碼** 。** [做更多的研究](// meta.stackoverflow.com/questions/261592)如果你有問題,你可以**發佈你已經嘗試過的**,並清楚說明什麼是不工作**並提供[** Minimal,Complete和Verifiable示例**](// stackoverflow.com/help/mcve)。我建議閱讀[問]一個好問題和[完美問題](http ://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,be肯定要參加[參觀]並閱讀** [this](// meta.stackoverflow.com/questions/347937/)**。 –

回答

1

您可以將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\":[\"[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); 

編輯: 只是改變這個curl_setopt($ CH,CURLOPT_POSTFIELDS,......與此

curl_setopt($ch, CURLOPT_POSTFIELDS,$senddata); 
+0

我更新了代碼。 如果我想將數組數據$ senddata發送到curl,該怎麼辦? – Tony

+0

請參閱編輯部分。 –

+0

謝謝,我試過了,但即使設置了電子郵件和數據,我也遇到了錯誤。 〜string'{「code」:「failure」,「message」:「valid」to'email address required,'data':[]}'(length = 74)〜 – Tony

0

您也可以下載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":["[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" 
    } 
}'); 

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

    echo $response->getBody(); 
} catch (HttpException $ex) { 
    echo $ex; 
}