How send attachment file by sendgrid using wix

462 views Asked by At

I wanna send email using wix.com by https://sendgrid.com and all work great, but the attached files come empty. Why?

I get file and send by send grid

<input type="file" id='uploadFile1' />
<div onclick='send()'>send</div>


<script>
import {fetch} from 'wix-fetch';  

function sendWithService(key, sender, recipient, subject, body, attach) {
 const url = "https://api.sendgrid.com/api/mail.send.json";

 const headers = {
   "Authorization": "Bearer " + key,
   "Content-Type": "application/x-www-form-urlencoded"
 };

 var contentfile = attach;
 const data = `from=${sender}&to=${recipient}&subject=${subject}&text=${body}&files[download.png]=${contentfile}`;

 const request = {
   "method": "post", 
   "headers": headers, 
   "body": data
 };

 return fetch(url, request).then(response => response.json());
}

function sendEmail(subject, body, attach) {
  const key = "FDkdfjls__MYUNIQKEY__dsfsdlfs__MYUNIQKEY__asfkjsdf3424";
  const sender = "[email protected]";
  const recipient = "[email protected]";
  return sendWithService(key, sender, recipient, subject, body, attach);
}

function send() {
  sendEmail('my subject', 'my text', document.getElementById('uploadFile1').files[0]).then(response => console.log(response));
}
</script>

email and key in this code was changed.

The answer is {message: "success"} and letters arrive correctly with subject and text, but the file is empty. It consists of either [object, object] or {}

0

There are 0 answers