forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Error in joining images #312

Closed AmbroiseVuaridel closed 6 years ago

AmbroiseVuaridel commented 6 years ago

I am trying to add images to email templates in an ejs file but get the following error:

Error: The file /Server/build/images/logo.png was not found!

(i removed the beggining of the path that initiate at the root of my drive)

here's html.ejs:

image

here's how i send the mail: 'use strict';

const nodeMailer = require("nodemailer"); const EmailTemplate = require('email-templates');

let smtpConfig = { host: CONFIG.email_host, port: CONFIG.email_port, secure: false, // upgrade later with STARTTLS auth: { user: CONFIG.email_user, pass: CONFIG.email_psw } };

var transporter = nodeMailer.createTransport(smtpConfig);

exports.sendPasswordReset = function (email, username, name, tokenUrl) { const email1 = new EmailTemplate({ message: { from: '"AMBROISE admin" ambroise@test.ch', // sender address to: email, }, send: true, preview: false, transport:transporter, views: { options: { extension: 'ejs' } } });

email1
    .send({
        template: 'resetPassword',
        locals: {
            name: name,
            username: username,
            token: tokenUrl
        }
    })
    .then(console.log)
    .catch(console.error);

};

and here's my file organization:

image

I don't understand why the image path is changed to /Server/build/images/logo.png while I call it in email and should be /Server/emails/images/logo.png

Any obvious error I made and missed ?

Thanks

niftylettuce commented 6 years ago

This is because juice uses something called webResources option, and it's not looking in the right folder. You would need to move the image to a different folder. See https://github.com/niftylettuce/email-templates/blob/master/src/index.js#L77. It's looking for a build folder right now (assuming you're on the latest version of email-templates). So the path it'll look at is /your-project/build/images/logo.png, and it's in /your-project/emails/resetPassword/images/logo.png.