【问题标题】:Sending email content as an attachment using PHP and SendGrid cURL使用 PHP 和 SendGrid cURL 将电子邮件内容作为附件发送
【发布时间】:2015-07-11 22:27:40
【问题描述】:

我正在构建一个基于 WordPress 的交付网站。在每笔交易中,我都会使用 SendGrid 和 cURL 发送一封包含 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);

有什么建议吗?谢谢之前

【问题讨论】:

    标签: php email sendgrid


    【解决方案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
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多