0

I am new to JavaScript. I am working with a website. In this website i have provide a functionality to the users to download a file. Now i need to know is there any way to send those downloaded file to email. My code for downloading content is given below.

function download2(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob)
    window.navigator.msSaveOrOpenBlob(file, filename);
else {
    var a = document.createElement("a"),
        url = URL.createObjectURL(file);
    a.href = url;
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    setTimeout(function () {
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
    }, 0);
 }
 };

and my send mail function is given below:

function sendMail(mail, name, roll, cls, sec, grp) {
var link = "mailto:" + mail
+ "?cc=ictpracticalslc@gmail.com"
+ "&subject=" + escape("AnswerSheet Submitted By " + name + " Class " + cls + " Section " + sec + " Group " + grp) 
+ "&body=" + escape("My Roll Is " + roll) 
; 
window.location.href = link;
};

when i call download2("<p>Hello</p>", "hello.html", "text/plain"). A file named hello.html is downloaded in my local machine. But i need to attach this file in sendMail function. so that users can send email with this attachment.

I need to update the sendMail function so that it can accept an attachment which is downloaded by download2 function. I am badly need this functionality. I am stuck here for 15 days and find no suitable solution. I need to do this with only javascript or jquery.

  • 1
    You can't; assuming that a `mailto:` link is fine, the only ways to do this without a server is to either a) tell the user to manually attach the downloaded file or b) use a 3rd party service –  Jul 20 '20 at 11:44
  • Can you please tell me how can i use 3rd party service? – Md. Nurul Huda Riyad Jul 20 '20 at 11:45
  • 1. Find a company that offers email services 2. pay them 3. use their API (probably not what you want) –  Jul 20 '20 at 11:47
  • Also, why do you need to do this with JS only? Don't you have a server? You've already spent 15 days on something that takes 15 minutes. –  Jul 20 '20 at 11:48
  • I don't have server. I host it to github. – Md. Nurul Huda Riyad Jul 20 '20 at 11:50
  • As I read it, you want the template to be opened by their mail client, but many people don't have a mail client installed / setup... – Austin T French Jul 20 '20 at 11:52
  • duplicate: [using mailto to send email with an attachment](https://stackoverflow.com/questions/5233556/using-mailto-to-send-email-with-an-attachment) –  Jul 20 '20 at 11:53
  • @AustinTFrench Those who using my system its a prerequisite to install a mail client. – Md. Nurul Huda Riyad Jul 20 '20 at 11:54
  • Does this answer your question? [Send email from static page hosted on GitHub Pages](https://stackoverflow.com/questions/24348223/send-email-from-static-page-hosted-on-github-pages) – Umair Khan Jul 20 '20 at 11:55
  • Why do you want to send an HTML attachment in the first place? What is the end goal here? emails support HTML, so what is the point of attaching it? –  Jul 20 '20 at 11:56
  • @ChrisG sorry bro. The mentioned link has a problem those have the clear path of the attachment file. But i don't have the exact path to downloading file. I have watched this before but not find any solution according to my situation. – Md. Nurul Huda Riyad Jul 20 '20 at 11:57
  • @ChrisG It is a website of exam sheet. Where student answer the multiple choice and send the html page to the teachers so that teacher can evaluate mark dynamically. I have a function for that to evaluate the marks. Answer Sheet evaluating works fine right now. – Md. Nurul Huda Riyad Jul 20 '20 at 12:00
  • Well, two things come to mind: a) don't use an HTML attachment but put the results in the email text b) use a service like Google Forms to collect the results instead. Also, the path of the attachment file is useless, that duplicate was supposed to point out that what you want to do is impossible. –  Jul 20 '20 at 12:03
  • And since it's not clear maybe: `mailto:` links open the computer's default email client; if the computer isn't set up accordingly, this won't work anyway. –  Jul 20 '20 at 12:05
  • @ChrisG Those who using my system its a prerequisite to install a mail client. – Md. Nurul Huda Riyad Jul 20 '20 at 12:07

0 Answers0