【问题标题】:Email a content with Link using Javascript使用 Javascript 发送带有链接的内容
【发布时间】:2013-04-16 18:44:04
【问题描述】:

我有一个 html 表单,其中包含用于添加新用户的输入字段。将用户添加到我的数据库后,使用我的 adduser.js 文件中的 sendmail() 函数将邮件发送给她/他。按照我的标准发送的邮件。但问题是我想在正文内容中添加一个超链接。 我的台词是这样的:

  sendMail(result.email, DbConfig.mailConfig.subject, "Dear" + " " + req.body.txtFirstName + req.body.txtLastName + ",\n Welcome to, COMPANY NAME " + txt.link('http://www.website.in') + "Your Login details are below: \n User name:" + req.body.txtLoginId + " \n Password:" + result.pwd)

但它没有像我预期的那样工作。我邮件中的结果是

Dear user.
Welcome to,COMPANY NAME<ahref="www.website.in"></a>.

它是这样来的。但是链接重定向到指定的目标。我的期望是:

Dear user.
Welcome to,COMPANY NAME.(on click of company name it redirects to targeted link).

我怎样才能做到这一点。我尝试在我的 JS 中直接使用标签。在我的情况下它也可以正常工作。

谢谢,

【问题讨论】:

  • 没有办法 javascript 可以发送这样的电子邮件...您可能正在使用 PhP ?
  • 你不能使用javascript发送电子邮件,你需要使用一些服务器端技术,比如php/java
  • 我正在使用节点,我有一个 json 文件
  • 当我在表单中添加我的名字时,我在收件箱中收到了邮件。
  • 您需要在邮件标题中指定 HTML 格式。

标签: javascript jquery html node.js jsonp


【解决方案1】:

假设 sendMail 功能正常工作,您需要在标题中指定电子邮件的“内容类型”。我不熟悉您正在使用的特定函数,但 PHP 的“邮件”函数的格式类似,并且需要第四个参数作为附加标题。

我想它可能会像这样工作:

var headers = 'Content-type: text/html; charset=iso-8859-1' + "\r\n";
var message = "Dear" + " " + req.body.txtFirstName + req.body.txtLastName + ",\n Welcome to, COMPANY NAME " + txt.link('http://www.website.in') + "Your Login details are below: \n User name:" + req.body.txtLoginId + " \n Password:" + result.pwd;

sendMail(result.email, DbConfig.mailConfig.subject, message, headers)

【讨论】:

    【解决方案2】:

    一种方法是在节点上emailjs。我冒昧地粘贴了他们的例子:

    $ npm install emails;
    //app.js:
    var email   = require("./path/to/emailjs/email");
    var server  = email.server.connect({
       user:    "username", 
       password:"password", 
       host:    "smtp.gmail.com", 
       ssl:     true
    });
    
    var message = {
       text:    "i hope this works", 
       from:    "you <username@gmail.com>", 
       to:      "someone <someone@gmail.com>, another <another@gmail.com>",
       cc:      "else <else@gmail.com>",
       subject: "testing emailjs",
       attachment: 
       [
          {data:"<html>i <i>hope</i> this works!</html>", alternative:true},
          {path:"path/to/file.zip", type:"application/zip", name:"renamed.zip"}
       ]
    };
    
    // send the message and get a callback with an error or details of the message that was sent
    server.send(message, function(err, message) { console.log(err || message); });
    
    // you can continue to send more messages with successive calls to 'server.send', 
    // they will be queued on the same smtp connection
    
    // or you can create a new server connection with 'email.server.connect' 
    // to asynchronously send individual emails instead of a queue
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多