Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Check wasm32-unknown-unknown
run: |
rustup target add wasm32-unknown-unknown
cargo check --verbose --target wasm32-unknown-unknown --no-default-features
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. This projec

## [2.0.1] - Unreleased

### Added
- `wasm32-unknown-unknown` support: the current time is read through `web-time` on that target, where `std::time::SystemTime::now()` panics. Build with `default-features = false`, since `gethostname` does not support it (#26).

### Fixed
- ASCII control characters other than tab, CR and LF in display names, group names and unstructured text headers are written as RFC 2047 encoded-words; they were written raw inside quoted strings and text (#53).
- Multipart bodies no longer start with an empty line before the first boundary (#14).
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ gethostname = ["dep:gethostname"]
gethostname = { version = "1.0.0", optional = true }
encodify = "1.0"

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
web-time = "1.1"

[dev-dependencies]
mail-parser = "1.0"
serde = { version = "1.0", features = ["derive"]}
serde_yaml = "0.9.10"
serde_json = "1.0"

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies]
wasm-bindgen-test = "0.3"

[lib]
doctest = false
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _mail-builder_ is a flexible **e-mail builder library** written in Rust. It incl
- Generates **e-mail** messages conforming to the Internet Message Format standard (_RFC 5322_).
- Full **MIME** support (_RFC 2045 - 2049_) with automatic selection of the most optimal encoding for each message body part.
- Fast **Base64**, **Quoted-Printable** and **RFC 2047** encoding through [`encodify`](https://crates.io/crates/encodify), its only required dependency (`gethostname` is optional).
- Runs on `wasm32-unknown-unknown` with `default-features = false` (the clock is read through [`web-time`](https://crates.io/crates/web-time) on that target).

Please note that this library does not support sending or parsing e-mail messages as these functionalities are provided by the crates [`mail-send`](https://crates.io/crates/mail-send) and [`mail-parser`](https://crates.io/crates/mail-parser).

Expand Down Expand Up @@ -167,6 +168,12 @@ To run the testsuite:
$ cargo test --all-features
```

to run the WebAssembly tests (requires [`wasm-pack`](https://rustwasm.github.io/wasm-pack/) and Node.js):

```bash
$ wasm-pack test --node --no-default-features
```

or, to run the testsuite with MIRI:

```bash
Expand Down
4 changes: 2 additions & 2 deletions src/headers/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/

use std::time::SystemTime;
use crate::{SystemTime, UNIX_EPOCH};

pub static DOW: &[&str] = &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
pub static MONTH: &[&str] = &[
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Date {
pub fn now() -> Self {
Self {
date: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0) as i64,
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ use std::{
#[cfg(feature = "gethostname")]
use std::sync::OnceLock;

#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub(crate) use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub(crate) use web_time::{SystemTime, UNIX_EPOCH};

use headers::{
Header, HeaderType,
address::Address,
Expand Down
2 changes: 1 addition & 1 deletion src/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

use crate::{
SystemTime, UNIX_EPOCH,
encoders::encode::BodyEncoding,
headers::{
Header, HeaderType, content_type::ContentType, message_id::MessageId, raw::Raw, text::Text,
Expand All @@ -16,7 +17,6 @@ use std::{
borrow::Cow,
cell::Cell,
sync::atomic::{AtomicU64, Ordering},
time::{SystemTime, UNIX_EPOCH},
};

/// MIME part of an e-mail.
Expand Down
22 changes: 22 additions & 0 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![cfg(all(target_family = "wasm", target_os = "unknown"))]

use mail_builder::MessageBuilder;
use wasm_bindgen_test::wasm_bindgen_test;

// Default Date, Message-ID and boundaries read the clock without panicking on wasm32-unknown-unknown.
#[wasm_bindgen_test]
fn builds_message_with_generated_headers() {
let message = MessageBuilder::new()
.from(("John Doe", "john@doe.com"))
.to("jane@doe.com")
.subject("Hello")
.text_body("text")
.html_body("<p>html</p>")
.write_to_string()
.unwrap();

assert!(message.contains("\r\nDate: "));
assert!(message.contains("Message-ID: <"));
assert!(message.contains("multipart/alternative"));
assert!(!message.contains("Date: Thu, 1 Jan 1970"));
}