Add this framework to your project and then use my Swift class:
class EmailSender : SKPSMTPMessageDelegate {
private init() {}
static let sharedInstance = EmailSender();
func sendEmail(email : String, subject : String, message : String) {
let EMAIL_FROM = "test@gmail.com";
let EMAIL_PASS = "TestPassword";
let SMTP_SERVER = "smtp.gmail.com";
let EMAIL_TO = email;
let emailMessage = SKPSMTPMessage();
emailMessage.delegate = self;
emailMessage.fromEmail = EMAIL_FROM;
emailMessage.toEmail = EMAIL_TO;
emailMessage.relayHost = SMTP_SERVER;
emailMessage.requiresAuth = true;
emailMessage.login = EMAIL_FROM;
emailMessage.pass = EMAIL_PASS;
emailMessage.subject = subject;
emailMessage.wantsSecure = true;
let plainMsg = [
kSKPSMTPPartContentTypeKey : "text/plain",
kSKPSMTPPartMessageKey : message,
kSKPSMTPPartContentTransferEncodingKey : "8bit"
];
emailMessage.parts = [plainMsg];
emailMessage.send();
}
//MARK SKPSMTPMessageDelegate
@objc func messageSent(_ message: SKPSMTPMessage!) {
}
@objc func messageFailed(_ message: SKPSMTPMessage!, error: Error!) {
}
}