我正在尝试使用nodemailer发送图像。但是,我要执行的操作是发送由html页面创建的图像,该图像由用户生成。
我能够生成页面的图像,并将其插入底部的页面中,并且能够发送电子邮件,但似乎无法通过电子邮件发送图像。
这是用于创建映像,然后将其发送到节点endpoing的脚本:
<script type="text/javascript" src="html2canvas/dist/html2canvas.js"></script>
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
<script type="text/javascript">
var doc = new jsPDF();
$(document).ready(function() {
$('#Submit').click(function(){
var doc = new jsPDF();
console.log("HERE");
html2canvas(document.querySelector("#Submit")).then(canvas => {
doc.addImage(canvas, 'JPEG', 10, 10, 160, 200, 'alias', 'NONE', 0);
var dataURL = canvas.toDataURL();
$.ajax({
type: 'POST',
url: 'http://192.168.1.23:8080/sendMail',
data: {"image": dataURL}
});
});
});
});
</script>
<button id="Submit" class="btn-secondary btn-sm back_to_options">Submit</button>
这是处理电子邮件发送的节点脚本。
let bodyParser = require('body-parser');
let multer = require('multer');
let cors = require('cors');
let url = require('url');
let express = require('express');
let app = express();
let nodemailer = require('nodemailer');
let fs = require('fs');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(multer());
/**
*
* Function to hopefully send an email :)
*/
app.post("/sendMail", function (req, res) {
// console.log(req.body.doc);
// console.log("\n");
// console.log(req.body.image);
var base64Data = req.body.image.replace(/^data:image\/png;base64,/, "");
// console.log(base64Data);
fs.writeFile("out.png", base64Data, 'base64', function(err) {
if(err) {
console.log(err);
}
});
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'user@gmail.com',
pass: 'pass!'
}
});
let mailOptions = {
from: 'user@gmail.com',
to: 'client@gmail.com',
subject: 'Test',
text: 'It works!',
attachments: [
{
path: req.body.image
},
{
filename: 'out.png',
path: 'out.png'
}
]
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
});
app.listen(8080);
在电子邮件中,两个图片均为空白。
有人对为什么会这样有任何想法吗?