2016-06-08 3 views
0

안녕하세요, SendGrid example을 (를) 사용하여 이메일을 보내려고합니다. 첨부 파일이있는 이메일이 전송됩니다.SendGrid API 첨부 파일이 올바르게로드되지 않습니다.

하지만 첨부 파일을 클릭하면 파일이 손상되었다는 메시지가 나타납니다. 첨부 파일의 원본 파일 크기는 768KB이지만 이메일 클라이언트에서 가져올 때 단지 0.1Kb입니다.

또한 첨부 파일이 PHP 응용 프로그램과 동일한 디렉토리에 있는지 확인했습니다.

내가 뭘 잘못하고 있니? 여기

는 laravel에서 나를 위해 작동, 나는 몇 가지 임시 폴더에 파일을 업로드하고 sendgrid하는 해당 경로를 제공하기 위해 노력 , 첨부 파일과 같은 문제가 내가

$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

를 사용하는 코드입니다. 여기에 코드를 첨부했습니다.

$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

관련 문제