2016-07-03 26 views
2

This is an answer當使用Mailgun的類的問題。我正在尋找一個適用於在PHP中使用CURL的答案。當通過PHP和CURL使用Mailgun時,PHPMailer的AddStringAttachment的等價物是什麼?


使用的PHPMailer的課,我可以通過以下方式發送多個附件:

$mail->AddStringAttachment($attachment1, $title1); 
$mail->AddStringAttachment($attachment2, $title2); 

,因爲我不是取出由服務器上的文件,並在一個字符串我不是作曲,我需要指定每個機箱的標題。


現在,我想通過PHP和CURL使用Mailgun來完成該操作。到目前爲止,我採用了以下技術用於發送郵件不帶附件:

$api_key="[my api key]"; 
$domain ="[my domain]"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    "from" => "[sender]", 
    "to" => $to, 
    "subject" => $subject, 
    "html" => $content 
)); 

在此之後相同的陣列中的指定字段的約定,什麼是使用PHP和捲曲與發送字符串的附件,並指定標題的等效Mailgun?

+0

爲什麼不使用Mailgun PHP api? –

+0

我可能不得不這樣做,但由於我只使用了MG的一些功能,我不介意讓一個庫更少。 –

+1

如果您只想添加一個附件,那麼只需將''attachment'=>'@/path/to/file.ext'添加到'CURLOPT_POSTFIELDS'數組中即可。但是他們的API指出,所有附件都使用表單名稱'attachment',因此如果不覆蓋以前的內容,就不能添加多個附件。在他們的API庫中,使用來自GuzzleHttp的代碼構造multipart/form-data請求,所以添加多個請求並不是問題。 – drew010

回答

1

我放棄了使用字符串的附件,而是創建了兩個臨時文件(基於先前由另一功能構成的內容)的臨時目錄(基於用戶的唯一ID的目錄名)的內部。 (感謝drew010開始我沿着正確的方向。)

我懷疑是下面的函數將是對別人有用,但也許各個部分將可幫助他人希望類似的功能。

function sendFormattedEmail ($coverNote, $attachment1, $title1, $attachment2, $title2) { 
    global $userID, $account; 

    if (!file_exists("temp_{$userID}")) { 
     mkdir("temp_{$userID}"); 
    } 

    file_put_contents("temp_{$userID}/{$title1}", $attachment1); 
    file_put_contents("temp_{$userID}/{$title2}", $attachment2); 

    $api_key="[api_key]"; 
    $domain ="[my_domain]"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
     "from" => "[my_return_address]", 
     "to" => $account, 
     "subject" => "your requested files", 
     "text" => $coverNote, 
     "attachment[1]" => new CurlFile("temp_{$userID}/{$title1}"), 
     "attachment[2]" => new CurlFile("temp_{$userID}/{$title2})" 
    )); 

    $response = curl_exec($ch); 
    $response = strtolower(str_replace("\n", "", trim($response))); 
    $result= json_decode($response, true); 
    $status = explode(".", $result["message"]); 

    if ($status[0] == "queued") { 
     echo json_encode(array ("result" => "success")); 
    } 
    else { 
     echo json_encode(array ("result" => "failure")); 
    } 

    curl_close($ch); 

    unlink ("temp_{$userID}/{$title1}"); 
    unlink ("temp_{$userID}/{$title2}"); 
    rmdir ("temp_{$userID}"); 
} 

如上所示,功能條以使得能夠使用json_encode的從Mailgun的響應中的換行字符。修剪和小寫轉換隻是我的偏好。

報告結果返回給調用函數後,它消除了兩個臨時文件,然後將臨時目錄。

相關問題