Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send emails in Swift using Mailgun

I have the need to send automatic emails in a Swift project I'm working on. So far my best lead seems to be using Mailgun. (I'm open to better options if anyone has liked something else)

Swift is not listed in the Mailgun API Reference in their documentation, and I didn't see objective-c either. The only article speaking at all about his I've found is this one.

Update

I've been trying to piece together everything and this is where I've gotten so far.

I was able to get Mailgun installed via cocoapods. Using it in Swift has been kinda tricky.

I setup cocoapods with the following pod file:

target 'TestApp' do
pod 'mailgun', '~> 1.0.3'
end

target 'TestAppTests' do
end

With this podfile I was able to run pod install and set up the dependencies. Then I setup an Objective-C-Bridging Header in build settings. I used the following objective-C bridging header.

#ifndef Promises_Promises_Bridging_Header_h
#define Promises_Promises_Bridging_Header_h
#import <mailgun/Mailgun.h>
#import "testMail.h"
#endif

I was getting a linking error for awhile, but I needed to have the project opened via the workspace and I had to go to Product -> Schemes -> Edit Schemes and add the Pods-mailgun to the top of the list and then it would let me build.

Now I want to take advantage of the MailGun API. The docs say to do the following.

Mailgun *mailgun = [Mailgun clientWithDomain:@"samples.mailgun.org" apiKey:@"key-3ax6xnjp29jd6fds4gc373sgvjxteol0"];

[mailgun sendMessageTo:@"Jay Baird <[email protected]>" 
                  from:@"Excited User <[email protected]>" 
               subject:@"Mailgun is awesome!" 
                  body:@"A unicode snowman for you! ☃"];
like image 805
Unome Avatar asked Apr 15 '15 04:04

Unome


People also ask

Is mailgun an SMTP?

Selecting A Free SMTP ServiceMailgun is an SMTP service provider for transactional email and email marketing campaigns, offering robust features, support, and analytics—for free!

How many emails can I send with mailgun?

5,000 messages/month are included. There is a limit of 300 messages per day on the included sandbox domain. Data retention for Logs and the Events API is 1 day. You cannot create custom domains.


1 Answers

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("[email protected]")
}
}

Is that correct?

like image 157
Tromos Avatar answered Sep 30 '22 01:09

Tromos