如何使用Mailgun在Swift发送电子邮件

如何使用Mailgun在Swift发送电子邮件

本文介绍了如何使用Mailgun在Swift发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要在我正在开展的Swift项目中发送自动电子邮件。到目前为止,我最好的线索似乎正在使用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在他们的文档中没有列在Mailgun API Reference中,我没有看到客观-c。唯一一篇关于他发现的文章,就是这个。

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.

我能够通过Cocapods安装Mailgun。在Swift中使用它一直很棘手。

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

我使用以下pod文件设置Cocapod:

I setup cocoapods with the following pod file:

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

target 'TestAppTests' do
end

使用此podfile,我可以运行pod安装并设置依赖关系。然后在构建设置中设置Objective-C桥接头。我使用以下目标C桥接头。

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

链接错误一段时间,但是我需要通过工作区打开项目,我必须去产品 - >方案 - >编辑方案,并将Pods-mailgun添加到列表的顶部,那么它会让我建立。

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.

现在我想利用MailGun API。文件说要执行以下操作。

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! ☃"];


推荐答案

此时我回答了我自己的问题。这个过程不是太可怕了使用cocoapods安装邮件枪。
使用objective-c桥接头链接所需的objective-c代码。
创建一个客观的c文件以容纳您的方法,这将调用邮件操作,并使用它。

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

然后在我的swift代码中,我只是调用: p>

Then in my swift code I just call:

            var test: mailTest = mailTest()
            test.sendMail("[email protected]")

这是mailTest头文件。我创建了一个实现sendMail方法的.m文件,并调用API中的代码,并且它的效果很好。唯一的其他可能是具有挑战性的事情是设置邮件帐户并配置您的域,以便可以发送电子邮件,但这不是代码相关的。

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.

这篇关于如何使用Mailgun在Swift发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:10