简体   繁体   English

如何使用JavaScript将文件附加到电子邮件?

[英]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.

相关问题 无法使用 smtpjs 将 PDF 文件附加到 JavaScript 中的 Email - Can’t Attach PDF File to Email in JavaScript Using smtpjs 如何在Chrome中使用Outlook应用程序将文件附加到电子邮件中 - how do i attach a file with email using outlook application in chrome 如何附加javascript文件并在构建dll文件时使用它们 - how to attach javascript file and use them when built dll file 如何使用 Javascript 格式化电子邮件中的 HTML? - How can I use Javascript to format HTML in an email? 如何在Javascript / Typescript中使用Storyblok电子邮件服务? - How can I use the Storyblok Email Service in Javascript / Typescript? 如何使用javascript将url参数附加到所有传出请求? - How can I use javascript to attach url parameters to all outgoing requests? 如何在 JavaScript 中附加窗口调整大小事件侦听器? - How can I attach a window resize event listener in JavaScript? 如何在JavaScript中将事件附加到父框架的元素? - How can I attach an Event to an element of a parent frame in javascript? 这是什么事,我怎么能附上一些东西? 使用Javascript - What event is this and how can I attach something to it? Javascript 如何使用链接而不是浏览按钮来附加文件? - How do I use a link instead of the browse button to attach a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM