Search code examples
greasemonkeytampermonkeyuserscriptssmtpjs

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


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("[email protected]",
                   "[email protected]",
                   "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)


Solution

  • 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.