使用 Nodemailer 和 Angular 将文件附加到表单
Attach file to the Form with Nodemailer and Angular
我按照 this Whosebug 的指导将文件附加到表格中。但是不断收到内部错误 500 和错误 400。我找不到任何具有类似任务的教程。如果你能陪我,我将不胜感激!
Server.js
var mailOptions = {
from: data.contactFrom, // sender address
contactEmail: data.contactEmail,
to: 'somebody@gmail.com', // list of receivers
subject: "Request for a Quote from " + data.contactName, // Subject line
contactMsg: data.contactMsg, // plaintext body
attachments: [{ filename: req.file.originalname, content: req.file.buffer]};
console.log(mailOptions)
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){ return console.log(error); }
console.log('Message sent: ' + info.response);
res.json(info);
});
Client.js
app.controller('QuoteCtrl', ['$scope','$http',
function($scope, $http) {
$scope.message = {};
$scope.sendMail = function () {
// //Include the file in AJAX request
var formData = new FormData();
Object.keys($scope.message).forEach(function(key) {
formData.append(key, $scope.message[key]);
});
$http.post('/send/sendQuote', formData, $scope.message, {
transformRequest: angular.identity, headers: {'Content-Type': undefined}}).
success(function(data) {
$scope.message = data;
});
$scope.message = {};
}
}]);
有哪些可能的错误以及可以让生活更轻松的改进措施?
这对我有用。我使用 FormData 包括输入文件和 req.body 值。
Server.js
router.post('/send', upload.array('attachment', 4),
function(req, res, next) {
//attaching files
var attachementList = [];
for (var i = 0; i < req.files.length; i++) {
attachementList.push({
filename: req.files[i].originalname,
path: req.files[i].path
});
}
var mailOption = {...}
transporter.sendMail(mailOptions, function(error, info){...});
Client.js
var formData = new FormData();
//select input file
var fileSelect = document.getElementById('file-select');
var files = fileSelect.files;
for(var i =0; i<files.length; i++) {
var file = files[i];
formData.append('attachment', file, file.name);
}
formData.append('bday', $scope.application.bday);
$http.post('/send/sendVoice', formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}, $scope.application).
success(function(data) {
$scope.application = data;
});
我按照 this Whosebug 的指导将文件附加到表格中。但是不断收到内部错误 500 和错误 400。我找不到任何具有类似任务的教程。如果你能陪我,我将不胜感激!
Server.js
var mailOptions = {
from: data.contactFrom, // sender address
contactEmail: data.contactEmail,
to: 'somebody@gmail.com', // list of receivers
subject: "Request for a Quote from " + data.contactName, // Subject line
contactMsg: data.contactMsg, // plaintext body
attachments: [{ filename: req.file.originalname, content: req.file.buffer]};
console.log(mailOptions)
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){ return console.log(error); }
console.log('Message sent: ' + info.response);
res.json(info);
});
Client.js
app.controller('QuoteCtrl', ['$scope','$http',
function($scope, $http) {
$scope.message = {};
$scope.sendMail = function () {
// //Include the file in AJAX request
var formData = new FormData();
Object.keys($scope.message).forEach(function(key) {
formData.append(key, $scope.message[key]);
});
$http.post('/send/sendQuote', formData, $scope.message, {
transformRequest: angular.identity, headers: {'Content-Type': undefined}}).
success(function(data) {
$scope.message = data;
});
$scope.message = {};
}
}]);
有哪些可能的错误以及可以让生活更轻松的改进措施?
这对我有用。我使用 FormData 包括输入文件和 req.body 值。
Server.js
router.post('/send', upload.array('attachment', 4),
function(req, res, next) {
//attaching files
var attachementList = [];
for (var i = 0; i < req.files.length; i++) {
attachementList.push({
filename: req.files[i].originalname,
path: req.files[i].path
});
}
var mailOption = {...}
transporter.sendMail(mailOptions, function(error, info){...});
Client.js
var formData = new FormData();
//select input file
var fileSelect = document.getElementById('file-select');
var files = fileSelect.files;
for(var i =0; i<files.length; i++) {
var file = files[i];
formData.append('attachment', file, file.name);
}
formData.append('bday', $scope.application.bday);
$http.post('/send/sendVoice', formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}, $scope.application).
success(function(data) {
$scope.application = data;
});