[英]How can I use JavaScript to attach a file to a email?
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? 有没有一种方法可以使用JavaScript自动将当前文件附加到电子邮件?
Thanks. 谢谢。
EDIT: Oh, and this has to work with IE7 and Outlook 2007, as well. 编辑:哦,这也必须与IE7和Outlook 2007一起使用。
Is there a way to use JavaScript to automatically attach the current file to a email?
有没有一种方法可以使用JavaScript自动将当前文件附加到电子邮件?
Nope, there isn't. 不,没有。 JavaScript runs entirely in the browser, and has no access to local files.
JavaScript完全在浏览器中运行,并且无法访问本地文件。 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. 可以使用
mailto:
链接启动默认的电子邮件客户端,并且可以预先设置主题和邮件正文。 But nothing beyond that. 但是除此之外。
Actually you can if you want it to work with MS technology as he described. 实际上,您可以按照他的描述将其与MS技术一起使用。 You can use ActiveX to interact with Outlook.
您可以使用ActiveX与Outlook进行交互。 See the question below.
请参阅下面的问题。
Problem creating an email with an attachment in Javascript 创建带有Java附件的电子邮件时出现问题
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(ie,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.. 尝试此代码。首先,您必须在Google Cloud Console中创建一个应用程序,然后从库中启用Gmail API。获取应用程序的凭据。为此,请单击“凭据”并在“授权重定向URI”位置,请忽略此链接https:// developers。 google.com/oauthplayground并保存。接下来在另一个标签中打开此链接https://developers.google.com/oauthplayground/单击右侧的设置符号。并在复选框上打勾(即,使用您自己的OAuth凭据)之后,您必须提供clientId和clientSecret。同时在左侧有一个带有占位符的文本框,例如“输入您自己的范围”,请保留此链接https://mail.google.com/并单击“授权” API,然后单击Exchange授权代码以获取令牌,然后将获得您的refreshToken和accessToken将这两个保留在您的代码中。希望这对您有所帮助。
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');
}
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.