简体   繁体   English

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

[英]How to send emails in Swift using Mailgun

I have the need to send automatic emails in a Swift project I'm working on. 我需要在我正在处理的Swift项目中发送自动电子邮件。 So far my best lead seems to be using Mailgun. 到目前为止,我最好的线索似乎是使用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. Swift的文档未在Mailgun API参考中列出,我也没有看到Objective-C。 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. 我能够通过cocoapods安装Mailgun。 Using it in Swift has been kinda tricky. 在Swift中使用它有点棘手。

I setup cocoapods with the following pod file: 我使用以下Pod文件设置了cocoapods:

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. 使用此podfile,我可以运行pod install并设置依赖项。 Then I setup an Objective-C-Bridging Header in build settings. 然后,在构建设置中设置一个Objective-C-Bridging标头。 I used the following objective-C bridging header. 我使用了以下Objective-C桥接标头。

#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. 我有一阵子出现链接错误,但是我需要通过工作区打开项目,我必须转到“ 产品”->“方案”->“编辑方案”,然后将Pods-mailgun添加到列表的顶部,然后它将让我建造。

Now I want to take advantage of the MailGun API. 现在,我想利用MailGun API。 The docs say to do the following. 文档说要执行以下操作。

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

[mailgun sendMessageTo:@"Jay Baird <jay.baird@rackspace.com>" 
                  from:@"Excited User <someone@sample.org>" 
               subject:@"Mailgun is awesome!" 
                  body:@"A unicode snowman for you! ☃"];

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. 但是我对您使用了多少个.h和.m文件以及每个文件包含什么感到困惑。 I am clueless on obj C so I try to follow you blindly. 我对obj C一无所知,所以我试图盲目地跟着你。

Correct me if I am wrong. 如果我错了,请纠正我。

• You have one .h bridging header file that contains: •您有一个.h桥接头文件,其中包含:

//
//  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: •您有一个.m文件,其中包含:

#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: •在Viewcontroller.swift中,您可以(在按钮中说):

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: 如果您希望尽可能多地站在Swift端,则可以尝试另一种选择:

  1. If you don't have a bridging header yet, create a dummy objective-c file (add new file to project). 如果您还没有桥接头,请创建一个虚拟的Objective-C文件(将新文件添加到项目中)。 Xcode will ask if you will like to add a header file (this will sort out the header for you automatically) Xcode会询问您是否要添加头文件(这将自动为您整理头文件)

  2. Add this to your bridging header (providing you already imported mail gun using cocoa pods): #import "mailgun/Mailgun.h" 将其添加到桥接头中( #import "mailgun/Mailgun.h"您已经使用可可豆荚导入了邮件枪): #import "mailgun/Mailgun.h"

  3. Import maligun in your swift class: import mailgun 在您的快速班中import mailgunimport mailgun

  4. Simply use mailgun's API in swift as you would in Objective-C: 只需像在Objective-C中那样迅速使用mailgun的API:

     @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. 使用cocoapods安装mailgun。 Link the objective-c code that is needed using an objective-c bridging header. 使用Objective-C桥接标头链接所需的Objective-C代码。 Create an objective c file to house your method that will call the mailgun operation, and use it. 创建一个目标c文件来容纳您的方法,该方法将调用mailgun操作,并使用它。

#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. 这是mailTest头文件。 I created a .m file that implements the sendMail method and calls the code from the API and it works great. 我创建了一个.m文件,该文件实现sendMail方法并从API调用代码,并且效果很好。 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帐户并配置您的域,以便可以发送电子邮件,但这与代码无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM