Introducing 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 means of 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 supplies easy-to-use APIs by means of Swift actors, simplifying authentication, safe connections, electronic mail retrieval, and sending.

Apparently, the vast majority of SwiftMail’s code was generated by Cursor utilizing its agent mode. With Cursor’s assist, I reached this state in below per week, drastically condensing what would have in any other case taken a number of weeks of handbook growth. My position turned one among chief architect, director, and infrequently chief roll-backer-to-a-good-state, since Cursor typically 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 gives foundational IMAP instructions and responses however was initially cumbersome attributable to heavy reliance on guarantees.
  • NIO SMTP Instance: Apple’s primary SMTP demonstration challenge, 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 SwiftIMAP

let imapServer = IMAPServer(host: "imap.instance.com", port: 993)
strive await imapServer.join()
strive await imapServer.login(username: "person@instance.com", password: "password")

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

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

strive await imapServer.logout()
strive await imapServer.shut()

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

Swift SMTP Instance:

import SwiftSMTP

let smtpServer = SMTPServer(host: "smtp.instance.com", port: 587)
strive await smtpServer.join()
strive await smtpServer.authenticate(username: "person@instance.com", password: "password")

let sender = EmailAddress(handle: "sender@instance.com", identify: "Sender Title")
let recipient = EmailAddress(handle: "recipient@instance.com", identify: "Recipient Title")
let electronic mail = Electronic mail(
    sender: sender,
    recipients: [recipient],
    topic: "Hi there from SwiftSMTP",
    physique: "This can be a check electronic mail despatched utilizing SwiftSMTP."
)

strive await smtpServer.sendEmail(electronic mail)
strive 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 site visitors at hint log degree, which is especially helpful for debugging. The included CLI demos ahead Swift Log messages to OSLog, which you’ll view conveniently in Console.app, categorized by IMAP_IN, IMAP_OUT, SMTP_IN, and SMTP_OUT. Allow detailed logging by setting the atmosphere variable ENABLE_DEBUG_OUTPUT=1.

Future Plans

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

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


Classes: Elements

Leave a Reply

Your email address will not be published. Required fields are marked *