简体   繁体   中英

How to apply cdnjs css or external css to nodemailer html template using jade & express

I'm trying to send mail using node mailer. Getting html mark up from jade template through external file.External file html is compiled using file stream and rendering properly whereas the style whichever inserted through maxcdn or cdnjs are not reflecting

in this case how to apply css or is there any other alternate way to achieve this scenario.

Jade Template

doctype html
html
  head
    title Application confirmation
    link(rel='stylesheet', type='text/css', href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css')
  body
    .container
      .row
        .col-md-4
          p Thank you for registering with us you applucation submitted succesfully
      .row
        .col-md-4
          p Thanks,
          p SnapCode Team

Node Server Code

var nodemailer = require('nodemailer');
var config_urls = require("../configfile");
var fs = require('fs');
var jade = require('jade');


function readFile() {
    var template = process.cwd() + '/public/mailTemplate/confirmation.jade';
    console.log('template', template);
    fs.readFile(template, 'utf-8', function(err, file) {
        if (err) {
            console.log('Template file not found!', template);
        } else {
            var compiledTmpl = jade.compile(file, { filename: template });
            htmlToSend = compiledTmpl(context);
            return htmlToSend;
        }
    });
}


module.exports = {

    SendMail: function(email, app_id, callback) {
        var template = process.cwd() + '/public/mailTemplate/confirmation.jade';
        fs.readFile(template, 'utf-8', function(err, file) {
            if (err) {
                console.log('Template file not found!', template);
            } else {
                var compiledTmpl = jade.compile(file, { filename: template });
                htmlToSend = compiledTmpl(context);

                var transporter = nodemailer.createTransport({
                    host: config_urls.url.host,
                    port: config_urls.url.port,
                    secure: true, 
                    auth: {
                        user: config_urls.url.gmailID,
                        pass: config_urls.url.gmailPassword
                    }
                });

                var mailOptions = {
                    from: config_urls.url.gmailID, 
                    to: email, 
                    subject: 'Application Confirmation', 
                    html: htmlToSend
                };

                transporter.sendMail(mailOptions, function(error, info) {
                    if (error) {
                        console.log(error);
                        return callback(Result = "false");
                    }
                    console.log('Message sent: ' + info.response);
                    return callback(Result = "true");
                });
            }
        });
    },
}; //end of function

As far as I know, be it PHP Mailing functions or node mailing functions(I have tried on both and worked with both), we cannot add external css. All we could do is write inline styles wherever it's necessary or write styles like

<style>
 //styles here
</style>

I hope that helps. Please do keep me updated. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM