php - 使用 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);
解决方案
此解决方案可能会对您有所帮助。
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);
这是参考网址
推荐阅读
- python - 在数据流入 GCP 时迭代存储桶内容
- reactjs - 我如何只希望单击的项目出现在模式上?
- python - Python 对 CSV 的请求
- java - Spring Boot 可运行 jar 中的 ANT 类路径
- vue.js - VueJS - 如何在同一个组件中使用道具来指定另一个组件中的值
- html - 如何删除输入类型文件的默认占位符并获取占位符,如下所示?
- angular - 无法连接到 test.mosquitto.org 进行测试
- c# - C#字符串中的引号和斜线标记
- raspberry-pi - 在树莓派上开始使用 reportlab
- xaml - 应用固定/静态 Listview 高度后,Listview 项目不滚动