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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

+1! And presetting subject and body does not work cross-browser.
Haha, imagine a world where e-mails were being send unnoticed by normal people surfing the web. spam²
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.
@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 stackoverflow.com/questions/14228703/…
@ColdCerberus here's a rundown on the File API: stackoverflow.com/questions/371875/… 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.
|
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

3 Comments

+1 Nice! It's proprietary technology and not suitable for everyday public use in most cases, but it may work for the OP. (Edit: I'm out of votes. Well, a virtual +1 then :)
@Pekka: I'll do that for you ;)
Thanks, this looks like it will work for me. I'm gonna try it tomorrow and see if it works. Thanks again.
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');
}
})
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.