aboutsummaryrefslogtreecommitdiffstats
path: root/tools/node_modules/nodemailer/lib/engines/smtp.js
blob: 99bd00e057ac73d3a5b11c25ff71f381ef688e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var wellKnownHosts = require("../wellknown"),
    simplesmtp = require("simplesmtp");

// Expose to the world
module.exports = SMTPTransport;

/**
 * <p>Generates a Transport object for SMTP</p>
 * 
 * <p>NB! This is a pool of connections that try to keep themselves alive. The
 * connection is not closed to the server once the message is delivered.</p>
 * 
 * <p>Possible options can be the following:</p>
 * 
 * <ul>
 *     <li><b>service</b> - a well known service identifier ("Gmail", "Hotmail"
 *         etc.) for auto-completing host, port and secure connection settings</li>
 *     <li><b>host</b> - hostname of the SMTP server</li>
 *     <li><b>port</b> - port of the SMTP server</li>
 *     <li><b>secureConnection</b> - use SSL</li>
 *     <li><b>name</b> - the name of the client server</li>
 *     <li><b>auth</b> - authentication object as <code>{user:"...", pass:"..."}</code>
 *     <li><b>ignoreTLS</b> - ignore server support for STARTTLS</li>
 *     <li><b>debug</b> - output client and server messages to console</li>
 *     <li><b>maxConnections</b> - how many connections to keep in the pool</li>
 * </ul> 
 * 
 * @constructor
 * @param {Object} [options] SMTP options
 */
function SMTPTransport(options){
    
    
    this.options = options || {};
    
    this.initOptions();
    
    this.pool = simplesmtp.createClientPool(this.options.port, 
        this.options.host, this.options);
}

/**
 * <p>Initializes the SMTP connection options. Needed mostly for legacy option
 * values and also for filling in the well known hosts data if needed.</p>
 */
SMTPTransport.prototype.initOptions = function(){
    var keys, key, i, len;
    
    // provide support for legacy API
    if(this.options.use_authentication === false){
        this.options.auth = false;
    }else if(this.options.user || this.options.pass || this.options.XOAuthToken){
        if(!this.options.auth){
            this.options.auth = {};
        }
        this.options.auth.user = this.options.auth.user || this.options.user;
        this.options.auth.pass = this.options.auth.pass || this.options.pass;
        this.options.auth.XOAuthToken = this.options.auth.XOAuthToken || this.options.XOAuthToken;
    }
    
    if(this.options.ssl){
        this.options.secureConnection = true;
    }
    
    if(this.options.tls === false){
        this.options.ignoreTLS = true;
    }
    
    // lets be modest just in case
    this.options.maxConnections = this.options.maxConnections || 5;
    
    // use well known settings if service is defined
    if(this.options.service && wellKnownHosts[this.options.service]){
        keys = Object.keys(wellKnownHosts[this.options.service]);
        for(i=0, len=keys.length; i<len; i++){
            key = keys[i];
            this.options[key] = this.options[key] ||
                    wellKnownHosts[this.options.service][key];
        }
    }
};

/**
 * <p>Forwards the mailcomposer message object to the simplesmpt client pool</p>
 * 
 * @param {Object} emailMessage MailComposer object
 * @param {Function} callback Callback function to run when the sending is completed
 */
SMTPTransport.prototype.sendMail = function(emailMessage, callback){
    // force SMTP encoding
    emailMessage.options.escapeSMTP = true;
    
    if(this.options.requiresAuth && 
      (!this.options.auth || !((this.options.auth.user && this.options.auth.pass) || this.options.auth.XOAuthToken))){
        return typeof callback == "function" && 
            callback(new Error("Authentication required, invalid details provided"));
    }
    
    this.pool.sendMail(emailMessage, callback);
};

/**
 * <p>Closes the client pool</p>
 * 
 * @param {Function} callback Callback function to run once the pool is closed
 */
SMTPTransport.prototype.close = function(callback){
    this.pool.close(callback);
};