Skip to content

Commit

Permalink
Replace PerfectSMTP dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Jun 14, 2024
1 parent 1bbff5b commit b57abab
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 56 deletions.
188 changes: 157 additions & 31 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,75 +1,201 @@
{
"pins" : [
{
"identity" : "perfect-copenssl",
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/Perfect-COpenSSL.git",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "ce3113e159b8c6d8565e5d8db2672b572c81aea9",
"version" : "4.0.2"
"revision" : "a22083713ee90808d527d0baa290c2fb13ca3096",
"version" : "1.21.1"
}
},
{
"identity" : "perfect-crypto",
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/Perfect-Crypto.git",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "39ddac516788294629dfdb509fa6c9d1e1808bb1",
"version" : "4.0.0"
"revision" : "7ece208cd401687641c88367a00e3ea2b04311f1",
"version" : "1.19.0"
}
},
{
"identity" : "perfect-curl",
"identity" : "console-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Agitek-io/Perfect-CURL.git",
"location" : "https://github.com/vapor/console-kit.git",
"state" : {
"branch" : "bug/curl_option_error",
"revision" : "e249f8493400f24b4566bd0504e71b58a8a9ffeb"
"revision" : "9f7932f22ab6f64aafadc14491e694179b7d0f6f",
"version" : "4.14.3"
}
},
{
"identity" : "perfect-libcurl",
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/Perfect-libcurl.git",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "b3d7e65ef5c27c0a027cdc621f34835975301bf1",
"version" : "2.1.0"
"revision" : "a31236f24bfd2ea2f520a74575881f6731d7ae68",
"version" : "4.7.0"
}
},
{
"identity" : "perfect-mime",
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/Perfect-MIME.git",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "247246122dd72a8f4d0c7becefb8d69869598bce",
"revision" : "8c9a227476555c55837e569be71944e02a056b72",
"version" : "4.9.1"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "ee97538f5b81ae89698fd95938896dec5217b148",
"version" : "1.1.1"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "bc1c29221f6dfeb0ebbfbc98eb95cd3d4967868e",
"version" : "3.4.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "9bee2fdb79cc740081abd8ebd80738063d632286",
"version" : "1.1.0"
}
},
{
"identity" : "perfect-smtp",
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-metrics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "ce594e71e92a1610015017f83f402894df540e51",
"version" : "2.4.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio",
"state" : {
"revision" : "9428f62793696d9a0cc1f26a63f63bb31da0516d",
"version" : "2.66.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras",
"state" : {
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
"version" : "1.22.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "8d8eb609929aee75336a0a3d2417280786265868",
"version" : "1.32.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl",
"state" : {
"revision" : "2b09805797f21c380f7dc9bedaab3157c5508efb",
"version" : "2.27.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "38ac8221dd20674682148d6451367f89c2652980",
"version" : "1.21.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-smtp",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sersoft-gmbh/swift-smtp.git",
"state" : {
"revision" : "46e9ef4c2a2808011aaf0ed62f9968da2b757a2d",
"version" : "2.10.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mailosaur/Perfect-SMTP.git",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"branch" : "mailosaur-smtp-client",
"revision" : "6a6bcb1e05cc25a789715a0eb7e5892d71bd5dcb"
"revision" : "f9266c85189c2751589a50ea5aec72799797e471",
"version" : "1.3.0"
}
},
{
"identity" : "perfect-thread",
"identity" : "vapor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/Perfect-Thread.git",
"location" : "https://github.com/vapor/vapor",
"state" : {
"revision" : "a50b9a33f08fce638f33ae3121613fe499cc23ec",
"version" : "3.0.7"
"revision" : "f1c3495b5c35ab67881412acf20144112729b560",
"version" : "4.101.3"
}
},
{
"identity" : "perfectlib",
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/PerfectlySoft/PerfectLib.git",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "859e696457fa91a9f998ad94080632f20b47fe58",
"version" : "4.0.1"
"revision" : "4232d34efa49f633ba61afde365d3896fc7f8740",
"version" : "2.15.0"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ let package = Package(
targets: ["Mailosaur"]),
],
dependencies: [
.package(url: "https://github.com/mailosaur/Perfect-SMTP.git", branch: "mailosaur-smtp-client")
.package(url: "https://github.com/sersoft-gmbh/swift-smtp.git", from: "2.10.0")
],
targets: [
.target(
name: "Mailosaur",
dependencies: []),
.testTarget(
name: "MailosaurTests",
dependencies: ["Mailosaur", .product(name: "PerfectSMTP", package: "Perfect-SMTP")],
dependencies: ["Mailosaur", .product(name: "SwiftSMTP", package: "swift-smtp")],
resources: [
.process("Resources/cat.png"),
.process("Resources/dog.png"),
Expand Down
2 changes: 1 addition & 1 deletion Tests/MailosaurTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class FilesTests: XCTestCase {
XCTAssertNotNil(rawEmail)
guard let rawEmail = rawEmail else { return }
XCTAssertTrue(rawEmail.count > 0)
XCTAssertTrue(rawEmail.contains(FilesTestsSetup.email.subject.data(using: .utf8)!.base64EncodedString()))
XCTAssertTrue(rawEmail.contains(FilesTestsSetup.email.subject))
}

func testGetAttachment() async throws {
Expand Down
69 changes: 47 additions & 22 deletions Tests/MailosaurTests/Tools/Mailer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
//

import Foundation
import NIO
import Mailosaur
import PerfectSMTP
import SwiftSMTP

class Mailer {
public static let shared = Mailer()
Expand All @@ -22,34 +23,58 @@ class Mailer {
let host = ProcessInfo.processInfo.environment["MAILOSAUR_SMTP_HOST"] ?? "mailosaur.net"
let port = ProcessInfo.processInfo.environment["MAILOSAUR_SMTP_PORT"] ?? "25"

let smtp = SMTPClient(url: "smtp://\(host):\(port)", requiresTLSUpgrade: true)
let email = EMail(client: smtp)
// let smtp = SMTPClient(url: "smtp://\(host):\(port)", requiresTLSUpgrade: true)
let smtpConfig = Configuration(server: .init(hostname: host,
port: Int(port),
encryption: .startTLS(.ifAvailable)),
connectionTimeOut: .seconds(5),
featureFlags: [.base64EncodeAllMessages, .maximumBase64LineLength64])

let evg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let mailer = SwiftSMTP.Mailer(group: evg, configuration: smtpConfig)

let randomString = self.getRandomString(length: 10)
let randomName = "\(randomString) \(randomString)"
let randomFromAddress = "\(randomString)@\(Self.sVerifiedDomain)"
let randomToAddress = sendToAddress ?? Servers.generateEmailAddress(serverId: server)

email.from = Recipient(name: "\(randomString) \(randomString)", address: randomFromAddress)
email.to = [Recipient(name: "\(randomString) \(randomString)", address: randomToAddress)]
email.subject = "\(randomString) subject"
email.text = Self.sText.replacingOccurrences(of: "REPLACED_DURING_TEST", with: randomString)
email.html = Self.sHtml.replacingOccurrences(of: "REPLACED_DURING_TEST", with: randomString)
let plainText = Self.sText.replacingOccurrences(of: "REPLACED_DURING_TEST", with: randomString)
let htmlText = Self.sHtml.replacingOccurrences(of: "REPLACED_DURING_TEST", with: randomString)

let cat = try Data(contentsOf: Bundle.module.url(forResource: "cat", withExtension: "png")!)
let dog = try Data(contentsOf: Bundle.module.url(forResource: "dog", withExtension: "png")!)

email.attachments.append(Attachment(path: Bundle.module.path(forResource: "cat", ofType: "png")!, contentId: "ii_1435fadb31d523f6"))
email.attachments.append(Attachment(path: Bundle.module.path(forResource: "dog", ofType: "png")!, contentId: "ii_1435fadb31d523f7"))
let email = Email(sender: .init(name: randomName, emailAddress: randomFromAddress),
replyTo: nil,
recipients: [
.init(name: randomName, emailAddress: randomToAddress),
],
subject: "\(randomString) subject",
body: .universal(plain: plainText, html: htmlText),
attachments: [
.init(name: "cat.png",
contentType: "image/png",
data: cat),
.init(name: "dog.png",
contentType: "image/png",
data: dog)
])

return try await withCheckedThrowingContinuation { continuation in
do {
try email.send() { code, header, body in
print(code)
print(header)
print(body)
continuation.resume(returning: ())
}
} catch (let error) {
print(error)
continuation.resume(throwing: error)
}
func _send(_ email: Email) async throws {
try await mailer.send(email)
}

do {
print("Sending mail...")
try await _send(email)
print("Successfully sent mail!")
} catch {
print("Failed sending: \(error)")
}
do {
try await evg.shutdownGracefully()
} catch {
print("Failed shutdown: \(error)")
}
}

Expand Down

0 comments on commit b57abab

Please sign in to comment.