使用PHP和SendGrid cURL

时间:2015-07-11 22:27:40

标签: php email sendgrid

我正在构建基于WordPress的投放站点。在每笔交易中,我都会使用SendGrid& amp ;;发送一封包含HTML内容的电子邮件。卷曲。该电子邮件也将传真给一些用户(使用RingCentral - 另一个第三方服务)。问题是,上下文也应作为附件发送。

如果我使用静态文件(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 个答案:

答案 0 :(得分: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
);