Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Created July 23, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathandnya/01fc6f7b78651c79f503333c69f75fa9 to your computer and use it in GitHub Desktop.
Save cathandnya/01fc6f7b78651c79f503333c69f75fa9 to your computer and use it in GitHub Desktop.
send mail via MailCore2
import Foundation
struct SendMail {
static let HOSTNAME = "smtp.gmail.com"
static let PORT: UInt32 = 465
static let USERNAME = "HOGEHOGE@gmail.com"
static let PASSWORD = "PASSWORD"
static let FROM = "HOGEHOGE@gmail.com"
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