Hedwig 是一個可以發(fā)送郵件到任何 SMTP 服務器的 Swift 跨平臺框架。
發(fā)送文本郵件:
let hedwig = Hedwig(hostName: "smtp.example.com", user: "foo@bar.com", password: "password") let mail = Mail( text: "Across the great wall we can reach every corner in the world.", from: "onev@onevcat.com", to: "foo@bar.com", subject: "Hello World" ) hedwig.send(mail) { error in if error != nil { /* Error happened */ } }發(fā)送HTML郵件:
let hedwig = Hedwig(hostName: "smtp.example.com", user: "foo@bar.com", password: "password") let attachement = Attachment(htmlContent: "<html><body><h1>Title</h1><p>Content</p></body></html>") let mail = Mail( text: "Fallback text", from: "onev@onevcat.com", to: "foo@bar.com", subject: "Title", attachments: [attachement] ) hedwig.send(mail) { error in if error != nil { /* Error happened */ } }抄送和暗抄送:
let hedwig = Hedwig(hostName: "smtp.example.com", user: "foo@bar.com", password: "password") let mail = Mail( text: "Across the great wall we can reach every corner in the world.", from: "onev@onevcat.com", to: "foo@bar.com", cc: "Wei Wang <onev@onevcat.com>, tom@example.com" // Addresses will be parsed for you bcc: "My Group: onev@onevcat.com, foo@bar.com;" // Even with group syntax subject: "Hello World" ) hedwig.send(mail) { error in if error != nil { /* Error happened */ } }SMTP設置:
let hedwig = Hedwig( hostName: "smtp.example.com", user: "foo@bar.com", password: "password", port: 1234, // Determined from secure layer by default secure: .plain, // .plain (Port 25) | .ssl (Port 465) | .tls (Port 587) (default) validation: .default, // You can set your own certificate/cipher/protocols domainName: "onevcat.com", // Used when saying hello to STMP Server authMethods: [.plain, .login] // Default: [.plain, .cramMD5, .login, .xOauth2] )發(fā)送圖片和附件:
let imagePath = "/tmp/image.png" // You can create an attachment from a local file path. let imageAttachment = Attachment( filePath: imagePath, inline: true, // Add "Content-ID" if you need to embed this image to another attachment. additionalHeaders: ["Content-ID": "hedwig-image"] ) let html = Attachment( htmlContent: "<html><body>A photo <img src=\"cid:hedwig-image\"/></body></html>", // If imageAttachment only used embeded in HTML, I recommend to set it as related. related: [imageAttachment] ) // You can also create attachment from raw data. let data = "{\"key\": \"hello world\"}".data(using: .utf8)! let json = Attachment( data: data, mime: "application/json", name: "file.json", inline: false // Send as standalone attachment. ) let mail = Mail( text: "Fallback text", from: "onev@onevcat.com", to: "foo@bar.com", subject: "Check the photo and json file!", attachments: [html, json] hedwig.send(mail) { error in if error != nil { /* Error happened */ } }發(fā)送給多個收件人:
let mail1: Mail = //... let mail2: Mail = //... hedwig.send([mail1, mail2], progress: { (mail, error) in if error != nil { print("\(mail) failed. Error: \(error)") } }, completion: { (sent, failed) in for mail in sent { print("Sent mail: \(mail.messageId)") } for (mail, error) in failed { print("Mail \(mail.messageId) errored: \(error)") } } )