2016-06-08 3 views
0

Привет Я пытаюсь отправить электронное письмо с помощью SendGrid example в php. Я получаю электронное письмо с приложением.Приложение API SendGrid загружается неправильно

Но когда я нажимаю на приложение, я получаю сообщение о том, что файл поврежден. Исходный размер файла вложения составляет 768 Кб, но когда я получаю его в почтовом клиенте, он равен всего 0.1 КБ.

Также я проверил и файл вложения находится в том же каталоге, что и приложение 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

Смежные вопросы