2015-07-11 146 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 
); 
相关问题