Use PHP functions in JavaScript

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;
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Abro, jQuery Webworker
19 Nov '11 Permalink

q  The given host 'research.nihonsoft.org' returns a 'Server not found'. Thats not your fault, but the Google results for "SSJS SendMail" aren't that informative and people should know what's going on here.

Gravatar
ffffffffff
2 Jun '11 Permalink

q  ffffffffffffffff

Gravatar
lh
9 Feb '11 Permalink

q  222222222222

Gravatar
a
21 Oct '10 Permalink

q  

hello



Contribute a New function

More functions

In this category

» mail

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !