首页 > 解决方案 > 使用 Sendgrid 从表单发送带有附件的电子邮件

问题描述

我正在尝试使用 Sendgrid Web api 将附件从表单发送到电子邮件。

但是,我不知道如何编辑以下代码以使用表单的输入文件。

请问有什么帮助吗?

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'the recipient email',
    'subject'   => 'New Careers',
    'html'      => $email,
    'text'      => $email,
    'from'      => 'the sender email',
  );



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


$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);

标签: phpemailsendgrid

解决方案


此解决方案可能会对您有所帮助。

if (function_exists('curl_file_create')) { // php 5.5+
  $cFile = curl_file_create($file_name_with_full_path);
} else { // 
  $cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

这是参考网址


推荐阅读