Global

Methods

WEmail(optopt) → {Promise}

Description:
  • 寄送email

    opt各欄位給予undefined或null時皆視為未給定而使用預設值;欄位型別或值不合規時,不連線即reject錯誤訊息字串(見下方「錯誤訊息」)。

    email檢核規則(srcEmail,以及toEmails、toEmailsCC、toEmailsBCC之每一筆皆適用):

    1. 須為字串,先去除前後空白。
    2. 網域(@之後)含非ASCII字元者視為國際化網域,先自動轉為punycode再檢核,並以轉換後之值寄送,例如user@例子.測試寄至user@xn--fsqu00a.xn--g6w251d;國際化網域內不得含空白、控制字元與/、\、?、#、%、:、@、[、]。
    3. 再以wsemi之isEmail檢核,須同時符合:
      • 全長至多254字元,帳號部分(@之前)至多64字元。
      • 帳號部分僅限ASCII之英數字與特殊字元 ! # $ % & ' * + / = ? ^ _ { | } ~ - 及反引號,點只能作為分隔,不得位於開頭、結尾或連續出現。
      • 網域為一段以上之主機名稱(英數字與中間之連字號,每段至多63字元,不得以連字號開頭或結尾)加頂級網域,頂級網域須為2字元以上之英文字母,或xn--開頭之punycode。
    4. 因此以下格式一律拒收:帶顯示名稱或角括號(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 email
    • srcName is not string、srcPW is not string、emTitle is not string、emContent is not string
    • srcHost is not effective string
    • srcPort is not valid port
    • toEmails is not string or array、toEmails is not email(以字串給予時)、toEmails[i] is not email(以陣列給予時,i為元素索引);toEmailsCC與toEmailsBCC同,僅欄位名不同
    • no recipients
    • emAttachments 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
Name Type Attributes Default Description
srcName String <optional>
'system by w-email'

輸入寄件人顯示名稱字串,可為數字,換行字元轉為空白並去除前後空白,給予空字串則不顯示名稱,預設'system by w-email'

srcEmail String

輸入寄件人email信箱字串,亦作為登入郵件伺服器之帳號,須符合email檢核規則

srcPW String <optional>
''

輸入寄件人email密碼字串,可為數字,郵件伺服器未要求登入時可為空字串,預設''

srcHost String <optional>
'smtp.gmail.com'

輸入郵件伺服器位址字串,去除前後空白後不可為空,預設'smtp.gmail.com'

srcPort Number | String <optional>
587

輸入郵件伺服器連接埠,可為正整數或其字串,範圍1至65535,465使用SSL/TLS連線,其他連接埠於伺服器支援時使用STARTTLS(伺服器未宣告STARTTLS時以明文連線與登入),預設587

emTitle String <optional>
''

輸入郵件名稱字串,可為數字,預設''

emContent String | Buffer <optional>
''

輸入郵件訊息字串,以html寄送,可為數字或Buffer,預設''

toEmails String | Array <optional>
[]

輸入收件人email字串或陣列,須符合email檢核規則與收件人規則,預設[]

toEmailsCC String | Array <optional>
[]

輸入副本收件人email字串或陣列,規則同toEmails,預設[]

toEmailsBCC String | Array <optional>
[]

輸入密件副本收件人email字串或陣列,規則同toEmails,預設[]

emAttachments Object | Array <optional>
[]

輸入附件物件或陣列,須符合附件規則,預設[]

Returns:

回傳Promise,resolve回傳郵件伺服器回應字串,部分收件人被伺服器拒收時仍為resolve且不回傳被拒名單;reject回傳錯誤,輸入檢核失敗時為錯誤訊息字串,其餘為Error物件(通常帶code,伺服器回應之錯誤另帶responseCode);toEmails、toEmailsCC、toEmailsBCC合計至少須有一位收件人;郵件伺服器無回應時依nodemailer預設逾時(連線2分鐘、招呼30秒、閒置10分鐘)後reject

Type
Promise