1

I am making a sheet for my class to use to record what they did each day.

At the end of the week, the people will need to email the file to their teacher.

Is there a way to use JavaScript to automatically attach the current file to a email?

Thanks.

EDIT: Oh, and this has to work with IE7 and Outlook 2007, as well.

Hawken MacKay Rives
  • 1,171
  • 1
  • 16
  • 26

3 Answers3

6

Is there a way to use JavaScript to automatically attach the current file to a email?

Nope, there isn't. JavaScript runs entirely in the browser, and has no access to local files. It is possible to start up the default E-Mail client using a mailto: link, and it is possible to pre-set a subject and message body. But nothing beyond that.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    +1! And presetting subject and body does not work cross-browser. – jwueller Nov 09 '10 at 19:40
  • Haha, imagine a world where e-mails were being send unnoticed by normal people surfing the web. spam² – Harmen Nov 09 '10 at 19:43
  • I'm looking for a resource to back this argument. It's a common issue for us developers to encounter clients that will not understand how it's not possible for javascript (or technically your browser) to achieve such functionality. Do you have any links you know that gives us this some kind of a legal/standard stuff that declares that this is actually real and should be expected by logical thinking and common sense? lol. – Alex Pappas Jan 10 '19 at 03:34
  • 1
    @ColdCerberus things have changed somewhat and theoretically JavaScript *can* read user files now under certain, tightly controlled conditions but there appears to be a clearly spelled out rule in the RFC prohibiting attaching them to a mailto link, see the accepted answer to this https://stackoverflow.com/questions/14228703/attaching-a-file-in-email-in-html5-web-page – Pekka Jan 11 '19 at 08:47
  • @Pekka웃 , Thank you so much for this. Unfortunately, i can't explain how, the link to the rfc provided, that i believe is supposed to be sufficient, is not convincing for the client and management (very unfortunate situation) given they are not that literate to this area. May I ask how "things have changed" now with JS where we can actually take files through JS? Is there another sort of internet protocol standard talking about this or maybe a link to the actual implementation of this on JS? I can't seem to find it on my own. Thank you so much again. – Alex Pappas Jan 14 '19 at 06:09
  • 1
    @ColdCerberus here's a rundown on the File API: https://stackoverflow.com/questions/371875/local-file-access-with-javascript But the RFC seems to be pretty clear on what can and can't go into a `mailto:` email even if you have access to the file's contents. What *might* be feasible is uploading the selected file's data to a web server using an Ajax request and sending the E-Mail from there. If that's not an option and it HAS to be sent through a local `mailto:` link that works for any client, it's just not doable. – Pekka Jan 14 '19 at 10:54
  • Thank you very much for this @Pekka웃. – Alex Pappas Jan 15 '19 at 01:27
2

Actually you can if you want it to work with MS technology as he described. You can use ActiveX to interact with Outlook. See the question below.

Problem creating an email with an attachment in Javascript

Community
  • 1
  • 1
jfrobishow
  • 2,897
  • 2
  • 27
  • 42
1

Try this code.First you have to create an app in Google Cloud Console and Enable Gmail API from library.Get the credentials of your app.For that click on Credentials and in the place of Authorized redirect URIskeep this link https://developers.google.com/oauthplayground and save it.Next in another tab open this link https://developers.google.com/oauthplayground/ click on settings symbol on right side.And make a tick on check box(i.e,Use your own OAuth credentials) after this You have to give your clientId and clientSecret.And at the sametime on left side there is a text box with placeholder like Input Your Own Scopes there keep this link https://mail.google.com/ and click on Authorize APIs then click on Exchange authorization code for tokens then you will get your refreshToken and accessToken keep these two in your code.Hope thsi helps for you..

const nodemailer=require('nodemailer');
const xoauth2=require('xoauth2');
var fs=require('fs');
var transporter=nodemailer.createTransport({
service:'gmail',
auth:{
    type: 'OAuth2',
    user:'Sender Mail',
clientId:'Your_clientId',//get from Google Cloud Console
clientSecret:'Your clientSecret',//get from Google Cloud Console
refreshToken:'Your refreshToken',//get from  https://developers.google.com/oauthplayground
accessToken:'Tor accessToken'//get from  https://developers.google.com/oauthplayground
},
});
fs.readFile("filePath",function(err,data){
var mailOptions={
from:' <Sender mail>',
to:'receiver mail',
subject:'Sample mail',
text:'Hello!!!!!!!!!!!!!',
attachments:[
{
    'filename':'filename.extension',//metion the filename with extension
     'content': data,
     'contentType':'application/type'//type indicates file type like pdf,jpg,...
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
    console.log('Error');
}
else{
console.log('Email Sent');
}
})
});
Syed Ayesha Bebe
  • 1,797
  • 1
  • 15
  • 26