Created
July 23, 2019 05:36
-
-
Save cathandnya/01fc6f7b78651c79f503333c69f75fa9 to your computer and use it in GitHub Desktop.
send mail via MailCore2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct SendMail { | |
static let HOSTNAME = "smtp.gmail.com" | |
static let PORT: UInt32 = 465 | |
static let USERNAME = "[email protected]" | |
static let PASSWORD = "PASSWORD" | |
static let FROM = "[email protected]" | |
static func send(subject: String, message: String, to email: [String], complection: ((Error?) -> Void)?) { | |
let smtpSession = MCOSMTPSession() | |
smtpSession.hostname = HOSTNAME | |
smtpSession.port = PORT | |
smtpSession.username = USERNAME | |
smtpSession.password = PASSWORD | |
smtpSession.authType = .saslPlain | |
smtpSession.connectionType = .TLS | |
let builder = MCOMessageBuilder() | |
builder.header.from = MCOAddress(displayName: nil, mailbox: FROM) | |
builder.header.to = email.map({ MCOAddress(displayName: nil, mailbox: $0) as Any }) | |
builder.header.subject = subject | |
builder.htmlBody = message | |
let data = builder.data() | |
let sendOperation = smtpSession.sendOperation(with: data) | |
sendOperation?.start({ (error) in | |
complection?(error) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment