How to send emails in Swift using Mailgun

不打扰是莪最后的温柔 提交于 2019-11-30 09:54:33

I think i am facing exactly the same problem with you. But I am a little confused on how many .h and .m files you have used and what each one contains. I am clueless on obj C so I try to follow you blindly.

Correct me if I am wrong.

• You have one .h bridging header file that contains:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "AFNetworking.h"
#import <mailgun/Mailgun.h>

• You have one .m file that contains:

#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>

@interface mailTest: NSObject
- (void) sendMail: (NSString*)email;

@end

• And in your Viewcontroller.swift you have (lets say in a button) this:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func myButton(sender: UIButton) {
    var test: mailTest = mailTest()
    test.sendMail("testemail@testemail.com")
}
}

Is that correct?

There's another option that you could try if you wanted as much as possible to be on the Swift side:

  1. If you don't have a bridging header yet, create a dummy objective-c file (add new file to project). Xcode will ask if you will like to add a header file (this will sort out the header for you automatically)

  2. Add this to your bridging header (providing you already imported mail gun using cocoa pods): #import "mailgun/Mailgun.h"

  3. Import maligun in your swift class: import mailgun

  4. Simply use mailgun's API in swift as you would in Objective-C:

    @IBAction func sendEmail(_ sender: Any) {
    
        let mailgun = Mailgun.client(withDomain: "samples.mailgun.org", apiKey: "key-3ax6xnjp29jd6fds4gc373sgvjxteol0")
    
        mailgun?.sendMessage(to: "Jay Baird <jay.baird@rackspace.com>", from: "Excited User <someone@sample.org>", subject: "Mailgun is awesome!", body: "A unicode snowman for you! ☃")
    
    }
    

At this point I've answered my own question. The process isn't too terrible. Install mailgun using cocoapods. Link the objective-c code that is needed using an objective-c bridging header. Create an objective c file to house your method that will call the mailgun operation, and use it.

#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>

@interface mailTest: NSObject
- (void) sendMail: (NSString*)email;

@end

Then in my swift code I just call:

            var test: mailTest = mailTest()
            test.sendMail("testemail@testemail.com")

Here is the mailTest header file. I created a .m file that implements the sendMail method and calls the code from the API and it works great. The only other thing that could be challenging is setting up the mailgun account and configuring your domain so that emails can be sent, but that isn't code related.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!