# JavaScript nodemailer
## 安裝方式
### Step 1
需要先安裝[**node.js**](https://nodejs.org/dist/v16.17.0/node-v16.17.0-x64.msi)
### Step 2
在終端機上輸入以下代碼
:::info
npm init -y
:::
然後
:::info
npm install nodemailer
:::
### Step 3
建立一個**index.js**
### Step 4
在**Google**帳號開啟兩步驟驗證,在開啟兩步驟驗證下方的應用程式密碼
:::warning

:::
### Step 5
應用程式名稱選其他,名稱隨意,然後好好保存密碼
## 開始學習
### 拉入nodemailer
```javascript
const nodemailer = require('nodemailer')
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
auth: {
user: 'Gmail帳號',
pass: '剛剛那組密碼',
},
})
transporter.sendMail({
from: 'Gmail帳號', // 寄件人
to: 'Gmail帳號', // 收件人 多位用,分開 例:'a@XXX, b@XXX, c@XXX'
}).then(info => {
console.log({ info })
}).catch(console.error)
```
### 加入信件主旨
```javascript
subject: '信件主旨',
```
### 加入文字內容
```javascript
text: '文字內容',
```
### 加入網頁內容
```javascript
html: '網頁內容',
```
### 加入檔案內容
單一檔案
```javascript
attachments: {
filename: '檔案',
path: '路徑'
},
```
多個檔案
```javascript
attachments: [{
filename: '檔案A',
path: '路徑A'
},
{
filename: '檔案B',
path: '路徑B'
},
]
```
### 編譯
在終端機輸入以下指令
:::info
node index
:::