0

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 = "my@email.com";
  const recipient = "my@email.co";
  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 {}

Bob Arnson
  • 21,377
  • 2
  • 40
  • 47
Fakt309
  • 821
  • 5
  • 14
  • I was using this tutorial: https://sendgrid.com/docs/API_Reference/Web_API/mail.html#-Send-a-test-specifying-the-file-content-type-by-appending-typemime-type-to-the-file-name – Fakt309 Aug 13 '20 at 17:43
  • But not sure i all do right – Fakt309 Aug 13 '20 at 17:43
  • 1
    Have you gone through this post? https://www.wix.com/corvid/forum/community-discussion/file-attachment-using-sendgrid-api – Meyer Aug 13 '20 at 19:46
  • 1
    wow, this post looks quite helpful, I didn't find it. I will definitely try to do it as written there, thank you very much. – Fakt309 Aug 13 '20 at 21:15

0 Answers0