Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yockow/swiftmailmessage
https://github.com/yockow/swiftmailmessage
Last synced: 24 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yockow/swiftmailmessage
- Owner: YOCKOW
- License: mit
- Created: 2021-04-10T09:59:22.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-11T05:46:01.000Z (about 2 years ago)
- Last Synced: 2023-03-02T05:27:00.712Z (over 1 year ago)
- Language: Swift
- Size: 59.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# What is `SwiftMailMessage`?
It will provide a representation of mail message.
It does *not* provide functions like a mailer.# Requirements
- Swift 5
- macOS(>=10.15) or Linux## Dependencies
![Dependencies](./dependencies.svg)
# Usage
You can get a text or data representation for a mail message like below.
- The output can be used for `sendmail -i -t`'s standard input.
- You can also use [Gmail API's Message](https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message) after the output is encoded with Base64.## Plain Text Message
```Swift
import MailMessagelet body = PlainText(
text: """
Hello, World!
こんにちは、世界!
""",
stringEncoding: .iso2022JP,
contentTransferEncoding: ._7bit
)
let message = MailMessage(
author: Person(displayName: "Author", mailAddress: "[email protected]"),
recipients: Group([Person(displayName: "Recipient", mailAddress: "[email protected]")]),
subject: "My First Mail Message. - 私の初めてのメールメッセージ -",
body: body
)print(try message.deliverableDescription(using: .iso2022JP))
/*
== OUTPUT ==
From: Author
To: Recipient
Subject: My First Mail Message. - =?iso-2022-jp?B?GyRCO2QkTj1pJGEbKEI=?=
=?iso-2022-jp?B?GyRCJEYkTiVhITwlayVhJUMlOyE8JTgbKEI=?= -
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=iso-2022-jpHello, World!
$B$3$s$K$A$O!"@$3&!*(B*/
```
## Rich Text Message (XHTML)
```Swift
import MailMessage
import XHTMLlet plainText = PlainText(
text: "Hello, HTML!",
contentTransferEncoding: .quotedPrintable
)
let xhtml = XHTMLDocument.template(
title: "XHTML Title",
contents: [
.div(children: [.text("XHTML")])
]
)
let htmlContent = RichText.HTMLContent(xhtml: xhtml)
let richText = RichText(plainText: plainText, htmlContent: htmlContent)let message = MailMessage(
recipients: Group([Person(mailAddress: "[email protected]")]),
subject: "Rich Text Mail",
body: richText
)print(try message.deliverableDescription())
/*
== OUTPUT ==
To: [email protected]
Subject: Rich Text Mail
Content-Type: multipart/alternative;
boundary="6v1YAWqdQCUybGGiWXME4RXQ--git.io/JOYPU"
Content-Transfer-Encoding: quoted-printable--6v1YAWqdQCUybGGiWXME4RXQ--git.io/JOYPU
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printableHello, HTML!
--6v1YAWqdQCUybGGiWXME4RXQ--git.io/JOYPU
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printableXHTML Title
XHTML
--6v1YAWqdQCUybGGiWXME4RXQ--git.io/JOYPU--
*/```
## Attach Files
```Swift
import MailMessage
import XHTMLlet plainText = PlainText(
text: "Hello, many resources!",
stringEncoding: .ascii,
contentTransferEncoding: .quotedPrintable
)
let pngFile = File(
filename: "samll.png",
contentType: MIMEType(pathExtension: .png)!,
contentID: ContentID(rawValue: "")!,
content: InputStream(url: pngFileURL)!
)
let xhtml = XHTMLDocument.template(
title: "title",
contents: [
.text("Hello, image!"),
.image(attributes: ["src": "cid:[email protected]"]),
]
)
let htmlContent = RichText.HTMLContent(xhtml: xhtml, resources: [pngFile])
let richText = RichText(plainText: plainText, htmlContent: htmlContent)
let body = FileAttachedBody(mainBody: richText, files: [shortTextFile])
let message = MailMessage(
recipients: Group([Person(mailAddress: "[email protected]")]),
subject: "Full Message",
body: body
)print(try message.deliverableDescription())
// OUTPUT omitted
```
# License
MIT License.
See "LICENSE.txt" for more information.