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.
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'
target 'TestAppTests' do
Mailgun *mailgun = [Mailgun clientWithDomain:@"samples.mailgun.org" apiKey:@"key-3ax6xnjp29jd6fds4gc373sgvjxteol0"];
[mailgun sendMessageTo:@"Jay Baird <email@example.com>"
from:@"Excited User <firstname.lastname@example.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("email@example.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.