2015-07-11 138 views
2

我正在構建一個基於WordPress的交付站點。在每次交易中,我都會使用SendGrid & cURL發送包含HTML內容的電子郵件。電子郵件也將傳真給一些用戶(使用RingCentral - 另一種第三方服務)。問題是,上下文也應作爲附件發送。使用PHP和SendGrid作爲附件發送電子郵件內容cURL

如果我使用靜態文件(test.txt),一切工作正常。

我需要發送內容("$email_heada {$_message[$eid]} $email_foota")作爲附件。有(多個用戶在同一時間發送電子郵件等)

下面是我當前的代碼的併發問題的可能性:

$fileName = 'test.txt'; 
$filePath = dirname(__FILE__); 

$params = array(
    'api_user' => $sendgridusername, 
    'api_key' => $sendgridpassword, 
    'to'  => $email, 
    'subject' => $mySubject, 
    'html'  => "$email_heada {$_message[$eid]} $email_foota", 
    'text'  => "$email_heada {$_message[$eid]} $email_foota", 
    'from'  => $settings_general->pear_user, 
    'files['.$fileName.']' => '@'.$filePath.'/'.$fileName 
); 

$request = $sendgridurl.'api/mail.send.json'; 

// Generate curl request 
$session = curl_init($request); 
// Tell curl to use HTTP POST 
curl_setopt ($session, CURLOPT_POST, true); 
// Tell curl that this is the body of the POST 
curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
// Tell curl not to return headers, but do return the response 
curl_setopt($session, CURLOPT_HEADER, false); 
// Tell PHP not to use SSLv3 (instead opting for TLS) 
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

// obtain response 
$response = curl_exec($session); 
curl_close($session); 

有什麼建議?感謝您需要加載該文件之前

回答

1

$fileName = 'test.txt'; 

$file = file_get_contents('./'.$fileName, true); 

$params = array(
    'api_user' => $sendgridusername, 
    'api_key' => $sendgridpassword, 
    'to'  => $email, 
    'subject' => $mySubject, 
    'html'  => "$email_heada {$_message[$eid]} $email_foota", 
    'text'  => "$email_heada {$_message[$eid]} $email_foota", 
    'from'  => $settings_general->pear_user, 
    'files['.$fileName.']' => '@'.$file.'/'.$fileName 
); 
相關問題