更改电子邮件正文的设计

时间:2016-09-22 07:38:55

标签: email meteor mailgun

我已经编写了在email.js中发送电子邮件的代码,如下所示:

Accounts.emailTemplates.siteName = "xyz";
Accounts.emailTemplates.from     = "xyz <admin@xyz.com>";

Accounts.emailTemplates.verifyEmail = {
    subject() {
        return "[xyz] Verify Your Email Address";
    }
};

Accounts.emailTemplates.verifyEmail.text = function( user, url) {
    let emailAddress   = user.emails[0].address,
    urlWithoutHash = url.replace( '', '' ),
    supportEmail   = "support@xyz.com",
    emailBody      = `To verify your email address (${emailAddress}) visit the following link:\n\n${urlWithoutHash}\n\n If you did not request this verification, please ignore this email. If you feel something is wrong, please contact our support team: ${supportEmail}.`;
    return emailBody;   
}

电子邮件正常运行,我想要的就是更改设计。如何设计电子邮件正文?我可以在电子邮件正文中插入html代码,以便我可以拥有正确的响应式电子邮件设计吗?我在很多方面都尝试过。有人可以帮帮我吗?

我使用邮件枪API发送电子邮件无论如何都要使用模板。

我尝试使用grunt电子邮件模板,我很震惊,我需要帮助来完成我的任务。

2 个答案:

答案 0 :(得分:0)

您可以使用SSR包创建电子邮件模板。

Accounts.emailTemplates.verifyEmail.html = function (user, url) {
       SSR.compileTemplate( 'registartionEmail', Assets.getText( 'email_templates/registration_confirm.html' ) );
       var emailData = {
              x: y;
       };
       return SSR.render( 'registartionEmail', emailData );
};

答案 1 :(得分:0)

要处理在服务器上将模板转换为原始HTML的过程,您需要向应用程序添加一个名为meteorhacks:ssr的包。一旦安装了软件包,就可以将纯HTML文件存储在/ private目录中,然后再转换它们,传递任何数据来替换过程中的{{name}}等句柄助手。

例如,以下是我用于在新用户注册时发送欢迎电子邮件的一些代码:

import { SSR } from 'meteor/meteorhacks:ssr';

const getHTMLForEmail = (templateName, data) => {
  SSR.compileTemplate(templateName, Assets.getText(`email/templates/${templateName}.html`));
  return SSR.render(templateName, data);
};

const sendEmail = (emailAddress, html) => {
  if (emailAddress.includes('@')) {
    const emailData = {
      to: emailAddress,
      from: 'Test Email <hello@test.io>',
      subject: 'Welcome aboard, team matey!',
      html,
    };

    Meteor.defer(() => Email.send(emailData));
  }
};

export const sendWelcomeEmail = (user, profile) => {
  let email;
  if (user.services.facebook) {
    email = user.services.facebook.email;
  } else if (user.services.google) {
    email = user.services.google.email;
  }

  const data = {
    email,
    name: profile && profile.name ? profile.name : '',
    url: 'www.google.com',
  };
  const html = getHTMLForEmail( 'welcome-email', data );

  sendEmail(data.email, html);
};

你会发现Meteor Chef的以下两篇文章非常有用(也展示了html电子邮件模板的样子):

  1. Using the email package
  2. Sign up with email verification