5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】Mailcore2を使ってアプリからメールを飛ばす

Last updated at Posted at 2020-05-03

Mailcore2を使ってアプリからメールを飛ばすsnippetです。
事前にCocoaPodsかCarthageを使ってインストールしてください。
データベースはFirebaseを使用しています。

ViewController.swift
func sendMail(email: String, name: String, orderId: String, orderText: String) {
            
            let smtpSession = MCOSMTPSession()
            smtpSession.hostname = "smtp.gmail.com"
            smtpSession.username = "example@gmail.com"
            smtpSession.password = "password"
            smtpSession.port = 465
            smtpSession.authType = MCOAuthType.saslPlain
            smtpSession.connectionType = MCOConnectionType.TLS
            smtpSession.connectionLogger = {(connectionID, type, data) in
                if data != nil {
                    if let string = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
                        NSLog("Connectionlogger: \(string)")
                    }
                }
            }
            let builder = MCOMessageBuilder()
            builder.header.to = [MCOAddress(displayName: name, mailbox: email)]
            builder.header.from = MCOAddress(displayName: "〇〇運営", mailbox: "example@gmail.com")
            builder.header.subject = "ご予約完了のお知らせ"
            builder.htmlBody="\(name)さん<br>
            〇〇をご利用いただきありがとうございます。<br>
            下記の〇〇のご予約が完了しました。<br>
            ■ご予約情報<br>
            オーダーID : (orderId)<br>
            受取人:<br>
            オーダー詳細:(orderText)<br>
            ■支払い金額<br>
            商品代金 : ¥ (price)<br>
            --------------------------<br>
            支払い金額 : ¥(price)<br>
            支払い方法 : クレジットカード<br>
            お届けまでしばらくお待ちください。<br>
            通常3日ほどでお届け致します。<br>
            サイトに戻る。<br>
            ※お問い合わせはexample@gmail.comまでお願い致します。"
            
            
            let rfc822Data = builder.data()
            let sendOperation = smtpSession.sendOperation(with: rfc822Data)
            sendOperation?.start { (error) -> Void in
                if (error != nil) {
                    NSLog("Error sending email: \(error)")
                    
                    
                } else {
                    NSLog("Successfully sent email!")
                    
                    
                }
            }
        }

            
        }
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?