Expand description
§ZeptoMail SDK for Rust
This is an unofficial Rust SDK for ZeptoMail, providing asynchronous abstractions to interact with the ZeptoMail API.
§Features
- Send Email: Send a single transactional email.
- Send Batch Email: Send a batch of transactional emails.
- Send Email with Template: Send a single email using a pre-defined template.
- Send Batch Email with Template: Send multiple emails using a template with recipient-specific merge data.
- File Upload to Cache: Upload files to ZeptoMail’s cache (for later use as attachments).
§Installation
Add the following to your Cargo.toml
:
[dependencies]
zeptomail_rs = "0.1.8"
Or use a local path dependency for testing:
[dependencies]
zeptomail_rs = { path = "../zeptomail_rs" }
§Usage
First, initialize the client with your Send Mail token. You can pass either a raw token or one prefixed with Zoho-enczapikey
.
let client = ZeptoMailClient::new("Zoho-enczapikey XXX").unwrap();
§Send a Single Email
Create an EmailRequest
. Note that the sender field is serialized as "from"
, and the recipient list is serialized as "to"
(each recipient is wrapped in a Recipient
struct).
use zeptomail_rs::{ZeptoMailClient, EmailRequest, EmailAddress, Recipient};
#[tokio::main]
async fn main() {
let client = ZeptoMailClient::new("Zoho-enczapikey XXX").unwrap();
let email_request = EmailRequest {
bounce_address: None,
sender: EmailAddress {
address: "info@example.com".to_string(),
name: Some("Info Example".to_string()),
},
to: vec![
Recipient {
email_address: EmailAddress {
address: "jon@example.com".to_string(),
name: Some("Jon Doe".to_string()),
},
merge_info: None,
}
],
reply_to: None,
subject: "Test Single Email".to_string(),
htmlbody: Some("<p>Sent from Rust Code (single email)</p>".to_string()),
textbody: Some("Plain text email".to_string()),
carbon_copy: None,
blind_carbon_copy: None,
track_clicks: Some(true),
track_opens: Some(true),
client_reference: None,
mime_headers: None,
attachments: None,
inline_images: None,
};
match client.send_email(email_request).await {
Ok(response) => println!("Email sent successfully: {:?}", response),
Err(e) => eprintln!("Error sending email: {:?}", e),
}
}
§Send Batch Emails
Use BatchEmailRequest
to send multiple emails in a single request. In this struct, the sender is renamed to "from"
and the recipients field is renamed to "to"
.
use zeptomail_rs::{ZeptoMailClient, BatchEmailRequest, EmailAddress, Recipient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ZeptoMailClient::new("Zoho-enczapikey XXX")?;
let batch_request = BatchEmailRequest {
sender: EmailAddress {
address: "info@example.com".to_string(),
name: Some("Info Example".to_string()),
},
recipients: vec![
Recipient {
email_address: EmailAddress {
address: "jon@example.com".to_string(),
name: Some("Jon Doe".to_string()),
},
merge_info: None,
},
Recipient {
email_address: EmailAddress {
address: "other@example.com".to_string(),
name: Some("Other Person".to_string()),
},
merge_info: None,
},
],
subject: "Test Batch Email".to_string(),
htmlbody: Some("<p>Sent from Rust Code (batch email)</p>".to_string()),
textbody: Some("Plain text batch email".to_string()),
carbon_copy: None,
blind_carbon_copy: None,
track_clicks: Some(true),
track_opens: Some(true),
client_reference: None,
mime_headers: None,
attachments: None,
inline_images: None,
};
match client.send_batch_email(batch_request).await {
Ok(response) => println!("Batch email sent successfully: {:?}", response),
Err(e) => eprintln!("Error sending batch email: {:?}", e),
}
Ok(())
}
§Send Email with Template
Use TemplateEmailRequest
to send a single email using a template. You must provide the template key, and you can also specify merge info that applies to all recipients.
use std::collections::HashMap;
use zeptomail_rs::{ZeptoMailClient, TemplateEmailRequest, EmailAddress, Recipient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ZeptoMailClient::new("Zoho-enczapikey XXX")?;
let template_request = TemplateEmailRequest {
template_key: "YOUR_TEMPLATE_KEY".to_string(),
bounce_address: None,
sender: EmailAddress {
address: "info@example.com".to_string(),
name: Some("Info Example".to_string()),
},
recipients: vec![
Recipient {
email_address: EmailAddress {
address: "jon@example.com".to_string(),
name: Some("Jon Doe".to_string()),
},
merge_info: None,
}
],
reply_to: Some(vec![
EmailAddress {
address: "reply@example.com".to_string(),
name: Some("Reply".to_string()),
}
]),
track_clicks: Some(true),
track_opens: Some(true),
client_reference: Some("template_email_ref".to_string()),
mime_headers: None,
attachments: None,
merge_info: Some(HashMap::new()),
};
match client.send_template_email(template_request).await {
Ok(response) => println!("Template email sent successfully: {:?}", response),
Err(e) => eprintln!("Error sending template email: {:?}", e),
}
Ok(())
}
§Send Batch Email with Template
Use BatchTemplateEmailRequest
to send multiple emails using a template. Like batch emails, the sender field is renamed to "from"
and the recipients field to "to"
.
use zeptomail_rs::{ZeptoMailClient, BatchTemplateEmailRequest, EmailAddress, Recipient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ZeptoMailClient::new("Zoho-enczapikey XXX")?;
let batch_template_request = BatchTemplateEmailRequest {
template_key: "YOUR_TEMPLATE_KEY".to_string(),
bounce_address: None,
sender: EmailAddress {
address: "info@example.com".to_string(),
name: Some("Info Example".to_string()),
},
recipients: vec![
Recipient {
email_address: EmailAddress {
address: "jon@example.com".to_string(),
name: Some("Jon Doe".to_string()),
},
merge_info: None,
},
Recipient {
email_address: EmailAddress {
address: "other@example.com".to_string(),
name: Some("Other Person".to_string()),
},
merge_info: None,
}
],
reply_to: Some(vec![
EmailAddress {
address: "reply@example.com".to_string(),
name: Some("Reply".to_string()),
}
]),
track_clicks: Some(true),
track_opens: Some(true),
client_reference: Some("batch_template_ref".to_string()),
mime_headers: None,
attachments: None,
};
match client.send_batch_template_email(batch_template_request).await {
Ok(response) => println!("Batch template email sent successfully: {:?}", response),
Err(e) => eprintln!("Error sending batch template email: {:?}", e),
}
Ok(())
}
§File Upload to Cache
Use FileUploadRequest
to upload a file to ZeptoMail’s cache. Unlike other endpoints, the file upload API requires a raw upload of the file’s bytes. The file name is provided as a query parameter and the Content-Type
header must match the file’s MIME type.
use zeptomail_rs::{ZeptoMailClient, FileUploadRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ZeptoMailClient::new("Zoho-enczapikey XXX")?;
let file_request = FileUploadRequest {
name: "example.txt".to_string(),
content_type: "text/plain".to_string(),
data: b"Hello, world!".to_vec(),
};
match client.upload_file_to_cache(file_request).await {
Ok(response) => println!("File uploaded successfully: {:?}", response),
Err(e) => eprintln!("Error uploading file: {:?}", e),
}
Ok(())
}
§Notes
- API Key: Pass your Send Mail token, with or without the
Zoho-enczapikey
prefix. - Bounce Address: Use a bounce address configured in your Mail Agent (or pass
None
if not needed). - Merge Info: For batch and template emails, you may supply merge data to personalize messages.
- File Upload: The file upload endpoint uses a query parameter for the file name and expects a raw body upload.
§Additional Operations
For detailed documentation on each operation, refer to the module-specific docs:
- Send Email
- Send Batch Email
- Send Email with Template
- Send Batch Email with Template
- File Upload to Cache
§License
MIT
§Repository
Re-exports§
pub use client::ZeptoMailClient;
pub use models::api_failure::ApiErrorDetail;
pub use models::api_failure::ApiError;
pub use models::api_failure::ZeptoMailError;
pub use models::api_failure::ZeptoMailErrorWrapper;
pub use models::api_success::SuccessData;
pub use models::api_success::ApiResponse;
pub use models::common::EmailAddress;
pub use models::common::Attachment;
pub use models::common::MimeHeaders;
pub use models::email::EmailRequest;
pub use models::email::BatchEmailRequest;
pub use models::email::Recipient;
pub use models::file_cache::FileUploadRequest;
pub use models::file_cache::FileUploadResponse;
pub use models::template::TemplateEmailRequest;
pub use models::template::BatchTemplateEmailRequest;
pub use models::email::InlineImage;