9

Can anybody tell me how to send mail with an attachment using JavaScript?

Smi
  • 13,850
  • 9
  • 56
  • 64
Mausmee Rai
  • 147
  • 1
  • 2
  • 5
  • 4
    @Samson it's not a duplicate, he's asking how to send an attachment, read the question better next time ;) – animaonline Oct 17 '14 at 08:58
  • Is it downvoted just because of selection of inappropriate technology? – naXa stands with Ukraine Dec 18 '15 at 10:07
  • 1
    This question has been marked as duplicate of [this](https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript) but, unless I am missing anything, that question does not have any mention of "attachment"s in email. I've cast a reopen vote. – Gaurang Tandon Dec 16 '17 at 10:52

4 Answers4

7

You have two options to send email:

  1. Use server side implementation to send email, access it in Javascript via XMLHttpRequest
  2. Open the local email client from JavaScript and user can send email with pre-populated data.

     var link = "mailto:target@example.com"; 
     // In addition to this you can add subject or body as parameter . 
     // For e.g. 
     // "mailto:target@example.com?subject=test subject&body=my text"
     window.location.href = link;
    
Mamun
  • 66,969
  • 9
  • 47
  • 59
18bytes
  • 5,951
  • 7
  • 42
  • 69
3

JavaScript is a client-side language. It is not concerned with (indeed, cannot) send e-mail, with or without an attachment. You'll need something server-side for that.

JavaScript can merely invoke the server-side script to send the e-mail, by requesting it, say, over AJAX, but it is not JavaScript which sends the e-mail.

This is akin to people mistakenly writing things like "I have some JavaScript which is getting some info from my database." It is not - it is requesting a server-side script which is getting the info from the database.

Mitya
  • 33,629
  • 9
  • 60
  • 107
2

With pure JavaScript, you can't send e-mails from the client. Remember, JavaScript is executed at the client (i. e. the user's browser).

grimmi
  • 21
  • 1
1

You can't send an email directly from Javascript. You could use it to do an AJAX call to send mail from whatever server side language you are using.

If you were using PHP:

Sending email via PHP

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137