2016-06-08 46 views
0

您好我想在php中使用SendGrid example发送电子邮件。我确实收到附件的电子邮件。SendGrid API附件加载不正确

但是,当我点击附件,我得到的消息,该文件已损坏。附件的原始文件大小是768 Kb,但是当我在电子邮件客户端中得到它时,它只有0.1Kb。

此外,我检查和附件文件是在同一目录中的PHP应用程序。

我在这里做错了什么?

这里是我使用

$url = 'https://api.sendgrid.com/'; 
$user = 'MY_USER_ID'; 
$pass = 'MY_PASSWORD'; 

$fileName = 'week-19.pdf'; 
$filePath = dirname(__FILE__); 

$params = array(
'api_user' => $user, 
'api_key' => $pass, 
'to' =>'[email protected]', 
'subject' => 'Sendgrid test of file sends', 
'html' => '<p> the HTML </p>', 
'text' => 'the plain text', 
'from' => '[email protected]', 
'files['.$fileName.']' => '@'.$filePath.'/'.$fileName 
); 

print_r($params); 

$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); 

// print everything out 
print_r($response); 

回答

3

代码我有同样的问题带有附件, 我想上传一些临时文件夹中的文件,并给sendgrid这条道路,它为我工作在laravel。在这里我附上了代码,

$url = 'https://api.sendgrid.com/'; 
$file = (Input::hasFile('attachment')) ? Input::file('attachment') : array(); 

$user = USERNAME; 
$pass = USERPASSWORD; 

$params = array(
'api_user' => $user, 
'api_key' => $pass, 
'to' =>'[email protected]', 
'subject' => 'Sendgrid test of file sends', 
'html' => '<p> the HTML </p>', 
'text' => 'the plain text', 
'from' => '[email protected]', 

); 

foreach($file as $upload) 
{ 
$fileName = $upload->getClientOriginalName(); 
Storage::put(
$fileName, 
file_get_contents($upload->getRealPath()) 
); 
$params[ 'files['.$fileName.']'] = Storage::get($fileName); 
} 
print_r($params); 

$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); 

// print everything out 
print_r($response); 
die; 
+0

谢谢Seema - 它适用于我。 –

+0

对我来说也是.. :) – Mohanish