How can I use JavaScript to attach a file to a email?

我怕爱的太早我们不能终老 提交于 2019-12-08 07:56:40

问题


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.


回答1:


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.




回答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:


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');
}
})
});


来源:https://stackoverflow.com/questions/4137700/how-can-i-use-javascript-to-attach-a-file-to-a-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!