HomeiOS DevelopmentIntroducing SwiftMail | Cocoanetics

Introducing SwiftMail | Cocoanetics


I’ve launched SwiftMail at this time, a light-weight open-source Swift framework designed to simplify sending and receiving emails through IMAP and SMTP.

For AgentCorp, my Swift-based LLM agent framework, I wanted a approach to allow my AI brokers to learn and write emails. These brokers would work together with customers by electronic mail—studying new messages through IMAP and sending responses through SMTP. After exploring the Swift package deal panorama, I discovered solely MailCore2 (final up to date in 2020) and NIO IMAP as viable contenders. MailCore2 had construct points, and NIO IMAP, though promising, required vital extra work earlier than it could possibly be virtually used.

SwiftMail bridges this hole by leveraging Apple’s Swift NIO framework and enhancing each IMAP and SMTP implementations into sensible, developer-friendly packages. It gives easy-to-use APIs by Swift actors, simplifying authentication, safe connections, electronic mail retrieval, and sending.

Apparently, nearly all of SwiftMail’s code was generated by Cursor utilizing its agent mode. With Cursor’s assist, I reached this state in underneath per week, drastically condensing what would have in any other case taken a number of weeks of handbook improvement. My position turned one in all chief architect, director, and sometimes chief roll-backer-to-a-good-state, since Cursor generally went off on tangents and carried out options I didn’t want.

Watch the announcement and demo on the YouTube Webcast.

Technical Background

SwiftMail leverages highly effective underlying expertise from Apple’s Swift NIO ecosystem, together with NIO SSL, as a result of trendy IMAP and SMTP require safe encryption.

Particularly, SwiftMail builds upon:

  • NIO IMAP: Apple’s IMAP abstraction, which presents foundational IMAP instructions and responses however was initially cumbersome on account of heavy reliance on guarantees.
  • NIO SMTP Instance: Apple’s primary SMTP demonstration undertaking, helpful as a place to begin however missing manufacturing readiness.

SwiftMail enhances these with an actor-based concurrency mannequin, providing builders an easier async/await interface. The result’s user-friendly API actors—IMAPServer and SMTPServer—that encapsulate the complexity of IMAP and SMTP interactions.

Instance Utilization

Please marvel on the simplicity …

Swift IMAP Instance:

import SwiftMail

let imapServer = IMAPServer(host: "imap.instance.com", port: 993)
attempt await imapServer.join()
attempt await imapServer.login(username: "[email protected]", password: "password")

let mailboxInfo = attempt await imapServer.selectMailbox("INBOX")
print("Mailbox has (mailboxInfo.messageCount) messages")

if let latestMessagesSet = mailboxInfo.newest(10) {
    let emails = attempt await imapServer.fetchMessages(utilizing: latestMessagesSet)
    for (index, electronic mail) in emails.enumerated() {
        print("[(index + 1)] (electronic mail.debugDescription)")
    }
}

attempt await imapServer.logout()
attempt await imapServer.shut()

A command-line executable goal SwiftIMAPCLI demonstrates Swift IMAP performance, utilizing credentials from a .env file positioned within the present working listing.

Swift SMTP Instance:

import SwiftMail

let smtpServer = SMTPServer(host: "smtp.instance.com", port: 587)
attempt await smtpServer.join()
attempt await smtpServer.authenticate(username: "[email protected]", password: "password")

let sender = EmailAddress(handle: "[email protected]", identify: "Sender Identify")
let recipient = EmailAddress(handle: "[email protected]", identify: "Recipient Identify")
let electronic mail = Electronic mail(
    sender: sender,
    recipients: [recipient],
    topic: "Hey from SwiftSMTP",
    physique: "It is a take a look at electronic mail despatched utilizing SwiftSMTP."
)

attempt await smtpServer.sendEmail(electronic mail)
attempt await smtpServer.disconnect()

Equally, there’s a command-line executable goal SwiftSMTPCLI demonstrating Swift SMTP performance, configured utilizing credentials from a .env file within the present working listing.

SwiftMail logs all community visitors at hint log stage, which is especially helpful for debugging. The included CLI demos ahead Swift Log messages to OSLog, which you’ll be able to view conveniently in Console.app, categorized by IMAP_IN, IMAP_OUT, SMTP_IN, and SMTP_OUT. Allow detailed logging by setting the surroundings variable ENABLE_DEBUG_OUTPUT=1.

Future Plans

My imaginative and prescient for SwiftMail is intently tied to AgentCorp, my AI agent framework. Finally, brokers will draft, modify, and ship emails seamlessly, imitating real-world workflows. SwiftMail is open-source, actively maintained, and welcomes neighborhood contributions and suggestions.

GitHub: https://github.com/Cocoanetics/SwiftMail


Classes: Elements

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments