Hedwig 是一个可以发送邮件到任何 SMTP 服务器的 Swift 跨平台框架。
发送文本邮件:
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 */ } }发送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] )发送图片和附件:
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 */ } }发送给多个收件人:
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)") } } )