Methods
WEmail(optopt) → {Promise}
- Description:
寄送email
opt各欄位給予undefined或null時皆視為未給定而使用預設值;欄位型別或值不合規時,不連線即reject錯誤訊息字串(見下方「錯誤訊息」)。
email檢核規則(srcEmail,以及toEmails、toEmailsCC、toEmailsBCC之每一筆皆適用):
- 須為字串,先去除前後空白。
- 網域(@之後)含非ASCII字元者視為國際化網域,先自動轉為punycode再檢核,並以轉換後之值寄送,例如
user@例子.測試寄至user@xn--fsqu00a.xn--g6w251d;國際化網域內不得含空白、控制字元與/、\、?、#、%、:、@、[、]。 - 再以wsemi之isEmail檢核,須同時符合:
- 全長至多254字元,帳號部分(@之前)至多64字元。
- 帳號部分僅限ASCII之英數字與特殊字元 ! # $ % & ' * + / = ? ^ _ { | } ~ - 及反引號,點只能作為分隔,不得位於開頭、結尾或連續出現。
- 網域為一段以上之主機名稱(英數字與中間之連字號,每段至多63字元,不得以連字號開頭或結尾)加頂級網域,頂級網域須為2字元以上之英文字母,或xn--開頭之punycode。
- 因此以下格式一律拒收:帶顯示名稱或角括號(
Alice <alice@example.com>、<alice@example.com>)、帶註解(bob@example.com (Bob))、一個字串內含多筆(以逗號、分號或空白分隔)、引號形式之帳號("john..doe"@example.com)、非ASCII之帳號(張三@example.com)、無頂級網域(user@localhost)、IP位址(user@[127.0.0.1])、網域含底線。
收件人規則(toEmails、toEmailsCC、toEmailsBCC):
- 可為一個email字串(僅一位收件人)或email字串陣列,多位收件人須以陣列給予。
- 給予null、undefined、false、0或空字串時,視為該欄無收件人;陣列內為falsy或空白字串之元素略過。
- 三欄合計至少須有一位收件人。密件副本(toEmailsBCC)不出現於信件標頭。
附件規則(emAttachments):
- 可為附件物件或附件物件陣列;給予null、undefined、false、0或空字串時視為無附件,陣列內為falsy或空白字串之元素略過。
- 每個附件物件須具下列內容來源之一:
raw(非空)、path(非空字串,本機路徑或網址)、href(非空字串網址)、content(字串、Buffer或可讀串流);filename若給予須為字串,或給予false表示不帶檔名。 - path與href會讀取本機檔案或連線下載,不可直接採用不可信任之輸入。
錯誤訊息(輸入檢核失敗時reject之字串,皆於連線前發生):
srcEmail is not emailsrcName is not string、srcPW is not string、emTitle is not string、emContent is not stringsrcHost is not effective stringsrcPort is not valid porttoEmails is not string or array、toEmails is not email(以字串給予時)、toEmails[i] is not email(以陣列給予時,i為元素索引);toEmailsCC與toEmailsBCC同,僅欄位名不同no recipientsemAttachments is not object or array、emAttachments is not valid attachment(以物件給予時)、emAttachments[i] is not valid attachment(以陣列給予時)
- Source:
Example
import WEmail from 'w-email'
//opt
let opt = {
srcName: 'test name',
srcEmail: 'your email',
srcPW: 'your password',
emTitle: 'test title',
emContent: '<h4>test head</h4>'+'<div>test content</div>',
toEmails: 'to email',
//toEmailsCC: 'to email',
//toEmailsBCC: 'to email',
emAttachments: {
filename: 'your filename',
path: 'your filepath'
}
}
//WEmail
new WEmail(opt)
.then((msg)=>{
console.log('then',msg)
})
.catch((err)=>{
console.log('catch',err)
})
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve回傳郵件伺服器回應字串,部分收件人被伺服器拒收時仍為resolve且不回傳被拒名單;reject回傳錯誤,輸入檢核失敗時為錯誤訊息字串,其餘為Error物件(通常帶code,伺服器回應之錯誤另帶responseCode);toEmails、toEmailsCC、toEmailsBCC合計至少須有一位收件人;郵件伺服器無回應時依nodemailer預設逾時(連線2分鐘、招呼30秒、閒置10分鐘)後reject
- Type
- Promise