Using SmtpJS in a userscript: Doesn't work, no error messages?

你说的曾经没有我的故事 提交于 2020-01-01 19:23:51

问题


I'm trying to send an email through a userscript using smtpjs because it seems the easiest approach. However it seems more difficult than just sending it by javascript that is embedded in a HTML page. Using this userscript (based on the smtpjs website) I get no error in the console and no email is sent, is this a framework issue or am I missing something here? (if you suggest an easier way to send emails within a userscript don't hesitate to share)

 // ==UserScript==
    // @name         New Userscript
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        *
    // @grant        none
    // @require      http://smtpjs.com/smtp.js
    // ==/UserScript==

    if (confirm("send mail?")) {
        Email.send("FROM@gmail.com",
                   "TO@gmail.com",
                   "This is a subject",
                   "this is the body",
                   "smtp.gmail.com",
                   "USER",
                   "PW");
    }

(I tried gmailAPI (pure JS version doesn't support sending emails?) and emailjs framework without success in userscripts)


回答1:


If you look at the smtpjs.com source, it creates a post request url, then appends it to the document inside of a <link>. This won't work on secure pages.

/* SmtpJS.com */
Email = {
  send: function (t, e, o, n, d, r, c) {
    var a = Math.floor(1e6 * Math.random() + 1),
     m = "http://smtpjs.com/smtp.aspx?";
     m += "From=" + t,
     m += "&to=" + e,
     m += "&Subject=" + encodeURIComponent(o),
     m += "&Body=" + encodeURIComponent(n),
     void 0 == d.token ?
       (m += "&Host=" + d, m += "&Username=" + r, m += "&Password=" + c, m += "&Action=Send") :
       (m += "&SecureToken=" + d.token, m += "&Action=SendFromStored"),
     m += "&cachebuster=" + a,
     Email.addScript(m) 
  },
  addScript: function (t) {
    var e = document.createElement("link");
    e.setAttribute("rel", "stylesheet"),
    e.setAttribute("type", "text/xml"),
    e.setAttribute("href", t),
    document.body.appendChild(e)
  }
};

You could use most of the above code... keep the send function, but replace the addScript function with a GM_xmlhttpRequest to post the data to their server.



来源:https://stackoverflow.com/questions/42125070/using-smtpjs-in-a-userscript-doesnt-work-no-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!