Home > AI > IOS >

send email

class Utils {
    
    static func mailGetURL(to: String = "noreply@jobyme88.com", subject: String, body: String) -> URL {
        let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
        let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
        
        
        let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
        let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)")
        let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
        let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
        let defaultUrl = URL(string: "mailto:\(to)?subject=\(subjectEncoded)&body=\(bodyEncoded)")
        
        
        if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) {
            return gmailUrl
        } else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) {
            return outlookUrl
        } else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) {
            return yahooMail
        } else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) {
            return sparkUrl
        }

        return defaultUrl!

    }
    
    static func mailOpen(url: URL){
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
    
    static func mailTo(to: String, subject: String, body: String) {
        let url = mailGetURL(to: to, subject: subject, body: body)
        mailOpen(url: url)
    }
    
    static func mailFeedback(){
        mailTo(to: "noreply@jobyme88.com", subject: "Enquries/Issues", body: "")
    }
    
    static func mailSharing() {
        mailTo(to: "",
               subject: "KidsLearnHindi - A great App for Kids to learn Hindi",
               body: "There is a great new App for kids to learn Hindi - KidsLearnHindi. \n\nCheck if out there - https://jobyme88.com")
    }
}

info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlegmail</string>
    <string>ms-outlook</string>
    <string>readdle-spark</string>
    <string>ymail</string>
</array>

Leave a Reply