1 | # SMTP transport module for Nodemailer
|
2 |
|
3 | Applies for Nodemailer v1.x and not for v0.x where transports are built-in.
|
4 |
|
5 | ## Setup
|
6 |
|
7 | Install with npm
|
8 |
|
9 | npm install nodemailer-smtp-transport
|
10 |
|
11 | Require to your script
|
12 |
|
13 | ```javascript
|
14 | var nodemailer = require('nodemailer');
|
15 | var smtpTransport = require('nodemailer-smtp-transport');
|
16 | ```
|
17 |
|
18 | ## Usage
|
19 |
|
20 | Create a Nodemailer transport object
|
21 |
|
22 | ```javascript
|
23 | var transporter = nodemailer.createTransport(smtpTransport(options))
|
24 | ```
|
25 |
|
26 | or (by using smtpTransport as default)
|
27 |
|
28 | ```javascript
|
29 | var transporter = nodemailer.createTransport(options)
|
30 | ```
|
31 |
|
32 | Where
|
33 |
|
34 | * **options** defines connection data
|
35 | * **options.port** is the port to connect to (defaults to 25 or 465)
|
36 | * **options.host** is the hostname or IP address to connect to (defaults to 'localhost')
|
37 | * **options.secure** defines if the connection should use SSL (if `true`) or not (if `false`)
|
38 | * **options.auth** defines authentication data (see [authentication](#authentication) section below)
|
39 | * **options.ignoreTLS** turns off STARTTLS support if true
|
40 | * **options.name** optional hostname of the client, used for identifying to the server
|
41 | * **options.localAddress** is the local interface to bind to for network connections
|
42 | * **options.connectionTimeout** how many milliseconds to wait for the connection to establish
|
43 | * **options.greetingTimeout** how many milliseconds to wait for the greeting after connection is established
|
44 | * **options.socketTimeout** how many milliseconds of inactivity to allow
|
45 | * **options.debug** if true, the connection emits all traffic between client and server as 'log' events
|
46 | * **options.authMethod** defines preferred authentication method, eg. 'PLAIN'
|
47 | * **options.tls** defines additional options to be passed to the socket constructor, eg. *{rejectUnauthorized: true}*
|
48 |
|
49 | **Example**
|
50 |
|
51 | ```javascript
|
52 | var transport = nodemailer.createTransport(smtpTransport({
|
53 | host: 'localhost',
|
54 | port: 25,
|
55 | auth: {
|
56 | user: 'username',
|
57 | pass: 'password'
|
58 | },
|
59 | maxConnections: 5,
|
60 | maxMessages: 10
|
61 | }));
|
62 | ```
|
63 |
|
64 | ## Authentication
|
65 |
|
66 | If authentication data is not present, the connection is considered authenticated from the start.
|
67 |
|
68 | Set authentcation data with `options.auth`
|
69 |
|
70 | Where
|
71 |
|
72 | * **auth** is the authentication object
|
73 | * **auth.user** is the username
|
74 | * **auth.pass** is the password for the user
|
75 | * **auth.xoauth2** is the OAuth2 access token (preferred if both `pass` and `xoauth2` values are set) or an [XOAuth2](https://github.com/andris9/xoauth2) token generator object.
|
76 |
|
77 | If a [XOAuth2](https://github.com/andris9/xoauth2) token generator is used as the value for `auth.xoauth2` then you do not need to set the value for `auth.user`. XOAuth2 generator generates required `accessToken` itself if it is missing or expired. In this case if the authentication fails, a new token is requested and the authentication is retried once. If it still fails, an error is returned.
|
78 |
|
79 | Install xoauth2 module to use XOauth2 token generators (not included by default)
|
80 |
|
81 | npm install xoauth2 --save
|
82 |
|
83 | **XOAuth2 Example**
|
84 |
|
85 | ```javascript
|
86 | var generator = require('xoauth2').createXOAuth2Generator({
|
87 | user: '{username}',
|
88 | clientId: '{Client ID}',
|
89 | clientSecret: '{Client Secret}',
|
90 | refreshToken: '{refresh-token}',
|
91 | accessToken: '{cached access token}' // optional
|
92 | });
|
93 |
|
94 | // listen for token updates
|
95 | // you probably want to store these to a db
|
96 | generator.on('token', function(token){
|
97 | console.log('New token for %s: %s', token.user, token.accessToken);
|
98 | });
|
99 |
|
100 | // login
|
101 | var transport = nodemailer.createTransport(smtpPool({
|
102 | service: 'gmail',
|
103 | auth: {
|
104 | xoauth2: generator
|
105 | },
|
106 | maxConnections: 5,
|
107 | maxMessages: 10
|
108 | }));
|
109 | ```
|
110 |
|
111 | ## Using well-known services
|
112 |
|
113 | If you do not want to specify the hostname, port and security settings for a well known service, you can use it by its name (case insensitive)
|
114 |
|
115 | If you do not want to specify the hostname, port and security settings for a well known service, you can use it by its name.
|
116 |
|
117 | ```javascript
|
118 | smtpTransport({
|
119 | service: 'gmail',
|
120 | auth: ..
|
121 | });
|
122 | ```
|
123 |
|
124 | See the list of all supported services [here](https://github.com/andris9/nodemailer-wellknown#supported-services).
|
125 |
|
126 | ## License
|
127 |
|
128 | **MIT**
|