JavaScript mail
Send an email message
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 75 | function mail (to, subject, message, additional_headers, additional_parameters) { // Send an email message // // version: 1109.2015 // discuss at: http://phpjs.org/functions/mail // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Currently only works if the SSJS SendMail method is available // % note 1: and also depends on the ini having been set for 'sendmail_from' // % note 2: 'additional_parameters' argument is not supported // * example 1: mail('you@example.com', 'Hi!', "What's been going on lately?"); // * returns 1: true // * example 2: mail("jack@example.com, barry@example.net", 'ok subj', 'my message', // * example 2: 'From: jack@example.com\r\n'+'Organization : Example Corp\r\n'+ // * example 2: 'Content-type: text/html;charset=utf8'); // * returns 2: true var _append = function (sm, prop, value) { if (!sm[prop]) { // Ok? sm[prop] = ''; sm[prop] += value; } else { sm[prop] += ',' + value; } }; if (this.window.SendMail) { // See http://research.nihonsoft.org/javascript/ServerReferenceJS12/sendmail.htm var sm = new this.window.SendMail(); var from = this.php_js && this.php_js.ini && this.php_js.ini.sendmail_from && this.php_js.ini.sendmail_from.local_value; sm.To = to; sm.Subject = subject; sm.Body = message; sm.From = from; if (additional_headers) { var headers = additional_headers.trim().split(/\r?\n/); for (var i = 0; i < headers.length; i++) { var header = headers[i]; var colonPos = header.indexOf(':'); if (colonPos === -1) { throw new Error('Malformed headers'); } var prop = header.slice(0, colonPos).trim(); var value = header.slice(colonPos + 1).trim(); switch (prop) { // Todo: Add any others to this top fall-through which can allow multiple headers // via commas; will otherwise be overwritten (Errorsto, Replyto?) case 'Bcc': // Fall-through case 'Cc': // Fall-through case 'To': // Apparently appendable with additional headers per PHP examples _append(sm, prop, value); break; case 'Subject': // Overridable in additional headers? break; case 'Body': // Overridable in additional headers? break; case 'From': // Default, though can be overridden /* Fall-through */ default: // Errorsto, Organization, Replyto, Smtpserver sm[prop] = value; break; } } } if (!sm.From) { throw new Error('Warning: mail(): "sendmail_from" not set in php.ini'); } return sm.send(); } return false; } |
Examples
» Example 1
Running
1 | mail('you@example.com', 'Hi!', "What's been going on lately?"); |
Should return
1 | true |
» Example 2
Running
1 2 3 | mail("jack@example.com, barry@example.net", 'ok subj', 'my message', 'From: jack@example.com\r\n'+'Organization : Example Corp\r\n'+ 'Content-type: text/html;charset=utf8'); |
Should return
1 | true |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have mail goodness in JavaScript.

ffffffffffffffff
Abro, jQuery Webworker
19 Nov '11