SendGrid-Curl php外部文件附件已损坏

SendGrid-Curl php外部文件附件已损坏,php,curl,email-attachments,sendgrid,Php,Curl,Email Attachments,Sendgrid,我将SendGrid用于一个客户项目,使用curl方法 一切正常,但我用SendGrid发送电子邮件时附带的(ir)文件已损坏 这是我的密码: $documentList = array( "DOC1.php" => "http://www.customerdomain.com/my/path/where/my/attachment/file/is/myfile.pdf" ); $params = array( 'api_user

我将SendGrid用于一个客户项目,使用curl方法

一切正常,但我用SendGrid发送电子邮件时附带的(ir)文件已损坏

这是我的密码:

$documentList = array(
    "DOC1.php" => "http://www.customerdomain.com/my/path/where/my/attachment/file/is/myfile.pdf"
);


$params = array(
                        'api_user'  => $user;
                        'api_key'   => $pass,
                        'x-smtpapi' => json_encode($json_string),
                        'from'      => $from,
                        'to'        => $to,
                        'subject'   => $subject,
                        'html'      => $mailHtml,
                        'text'      => $mailText
);


if(count($documentList)>0){
    foreach($documentList as $fileName=>$documentPath){
        $params['files['.$fileName.']'] =  $documentPath;
    }
}

$request =  $url.'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);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);


// obtain response
$response = curl_exec($session);
curl_close($session);
当我的数组键上没有扩展文件时,我有一个包含相关值的文本文件


我想我不是唯一一个遇到这个问题的人,如果你有任何解决这个问题的想法,谢谢你的帮助

您遇到的问题是,您给SendGrid的是文件的URL,而不是文件本身,SendGrid的API需要该文件

要使代码正常工作,只需将
$documentList
变量更改为:

$documentList = array(
    "DOC1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
有关此类文件上载的说明可以在中找到,但您可能需要使用来完成此操作


但是,最好/最简单的方法可能是使用以下方法:

require(“path/to/sendgrid-php/sendgrid-php.php”);
$sendgrid=newsendgrid('username','password');
$email=new SendGrid\email();
$email->addTo($email)foo@bar.com')->
设置自('me@bar.com')->
setSubject('主题在这里')->
setText(“你好,世界!”)->
setHtml(“你好,世界!”)
添加附件(“../path/to/file.txt”);
$sendgrid->send($email);

您遇到的问题是,您给SendGrid的是文件的URL,而不是文件本身,SendGrid的API需要该文件

要使代码正常工作,只需将
$documentList
变量更改为:

$documentList = array(
    "DOC1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
有关此类文件上载的说明可以在中找到,但您可能需要使用来完成此操作


但是,最好/最简单的方法可能是使用以下方法:

require(“path/to/sendgrid-php/sendgrid-php.php”);
$sendgrid=newsendgrid('username','password');
$email=new SendGrid\email();
$email->addTo($email)foo@bar.com')->
设置自('me@bar.com')->
setSubject('主题在这里')->
setText(“你好,世界!”)->
setHtml(“你好,世界!”)
添加附件(“../path/to/file.txt”);
$sendgrid->send($email);

您遇到的问题是,您给SendGrid的是文件的URL,而不是文件本身,SendGrid的API需要该文件

要使代码正常工作,只需将
$documentList
变量更改为:

$documentList = array(
    "DOC1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
有关此类文件上载的说明可以在中找到,但您可能需要使用来完成此操作


但是,最好/最简单的方法可能是使用以下方法:

require(“path/to/sendgrid-php/sendgrid-php.php”);
$sendgrid=newsendgrid('username','password');
$email=new SendGrid\email();
$email->addTo($email)foo@bar.com')->
设置自('me@bar.com')->
setSubject('主题在这里')->
setText(“你好,世界!”)->
setHtml(“你好,世界!”)
添加附件(“../path/to/file.txt”);
$sendgrid->send($email);

您遇到的问题是,您给SendGrid的是文件的URL,而不是文件本身,SendGrid的API需要该文件

要使代码正常工作,只需将
$documentList
变量更改为:

$documentList = array(
    "DOC1.pdf" => "@" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
有关此类文件上载的说明可以在中找到,但您可能需要使用来完成此操作


但是,最好/最简单的方法可能是使用以下方法:

require(“path/to/sendgrid-php/sendgrid-php.php”);
$sendgrid=newsendgrid('username','password');
$email=new SendGrid\email();
$email->addTo($email)foo@bar.com')->
设置自('me@bar.com')->
setSubject('主题在这里')->
setText(“你好,世界!”)->
setHtml(“你好,世界!”)
添加附件(“../path/to/file.txt”);
$sendgrid->send($email);

我最初的问题是因为路径是自动生成的链接,这就是为什么我没有使用比realpath更高的url

无论如何,我更改了代码,现在使用文件realpath(和@before)后面提到的带有mimetype的realpath

现在看来工作不错

我要感谢你的帮助


关于

我最初的问题是因为路径是自动生成的链接,这就是为什么我没有使用比realpath更合适的url

无论如何,我更改了代码,现在使用文件realpath(和@before)后面提到的带有mimetype的realpath

现在看来工作不错

我要感谢你的帮助


关于

我最初的问题是因为路径是自动生成的链接,这就是为什么我没有使用比realpath更合适的url

无论如何,我更改了代码,现在使用文件realpath(和@before)后面提到的带有mimetype的realpath

现在看来工作不错

我要感谢你的帮助


关于

我最初的问题是因为路径是自动生成的链接,这就是为什么我没有使用比realpath更合适的url

无论如何,我更改了代码,现在使用文件realpath(和@before)后面提到的带有mimetype的realpath

现在看来工作不错

我要感谢你的帮助

问候