| Crates.io | azure-ecs-rs |
| lib.rs | azure-ecs-rs |
| version | 0.1.1 |
| created_at | 2024-11-14 12:53:18.36038+00 |
| updated_at | 2024-11-15 12:52:34.766499+00 |
| description | Azure ECS (Email Communication Service) Rust SDK |
| homepage | https://github.com/preedep/azure-ecs-rs |
| repository | https://github.com/preedep/azure-ecs-rs |
| max_upload_size | |
| id | 1447783 |
| size | 143,344 |
Azure Email Communication Service is part of the Azure Communication Services. It provides a REST API to send emails. For more information, see the Azure Communication Services documentation.
This crate provides a Rust client library for the Azure Email Communication Service. It supports the following features: Azure Communication Service - Email - Rest API
Core Features
Support Authentication:
Shared Key
Service Principle
Managed Identity
Before running the examples, you need to set the following environment variables:
# For Common
SENDER="xxx
REPLY_EMAIL="xxxx"
REPLY_EMAIL_DISPLAY="xxxx"
# For Shared Key
CONNECTION_STR="xxxxx"
# For SMTP
SMTP_USER="xxxx"
SMTP_PASSWORD="xxxx"
SMTP_SERVER="smtp.azurecomm.net"
# For Service Principle
CLIENT_ID="xx"
CLIENT_SECRET="xxx"
TENANT_ID="xxx"
# Host name
ASC_URL="https://xxxxx.asiapacific.communication.azure.com"
My example code is in the examples directory. You can run the examples with:
# For simple email
RUST_LOG=debug cargo run --example mail
# For simple email with async
RUST_LOG=debug cargo run --example mail_async
# For email with attachment
RUST_LOG=debug cargo run --example mail_attach
How to use the library:
Shared Key
Get from Azure Portal
CONNECTION_STR

SENDER

let connection_str = get_env_var("CONNECTION_STR");
let acs_client_builder = ACSClientBuilder::new().connection_string(connection_str.as_str())
let host_name = get_env_var("ASC_URL");
let tenant_id = get_env_var("TENANT_ID");
let client_id = get_env_var("CLIENT_ID");
let client_secret = get_env_var("CLIENT_SECRET");
let acs_client_builder = ACSClientBuilder::new()
.host(host_name.as_str())
.service_principal(
tenant_id.as_str(),
client_id.as_str(),
client_secret.as_str(),
)
let host_name = get_env_var("ASC_URL");
let acs_client_builder = ACSClientBuilder::new()
.managed_identity()
.host(host_name.as_str())
let email_request = SentEmailBuilder::new()
.sender(sender.to_owned())
.content(EmailContent {
subject: Some("An exciting offer especially for you!".to_string()),
plain_text: Some("This exciting offer was created especially for you, our most loyal customer.".to_string()),
html: Some("<html><head><title>Exciting offer!</title></head><body><h1>This exciting offer was created especially for you, our most loyal customer.</h1></body></html>".to_string()),
})
.recipients(Recipients {
to: Some(vec![EmailAddress {
email: Some(recipient.to_owned()),
display_name: Some(display_name.to_owned()),
}]),
cc: None,
b_cc: None,
})
.user_engagement_tracking_disabled(false)
.build()
.expect("Failed to build SentEmail");
debug!("Email request: {:#?}", email_request);
let acs_client = acs_client_builder
.build()
.expect("Failed to build ACSClient");
let resp_send_email = acs_client.send_email(&email_request).await;
let resp_send_email = acs_client.send_email(&email_request).await;
match resp_send_email {
Ok(message_resp_id) => {
info!("Email was sent with message id: {}", message_resp_id);
loop {
tokio::time::sleep(time::Duration::from_secs(5)).await;
let resp_status = acs_client.get_email_status(&message_resp_id).await;
if let Ok(status) = resp_status {
info!("{}\r\n", status.to_string());
if matches!(
status,
EmailSendStatusType::Unknown
| EmailSendStatusType::Canceled
| EmailSendStatusType::Failed
| EmailSendStatusType::Succeeded
) {
break;
}
} else {
error!("Error getting email status: {:?}", resp_status);
break;
}
}
}
Err(e) => error!("Error sending email: {:?}", e),
}