-
Hello Could you please clarify how one sends an email? Once you have the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Generally I recommend going by examples in the official documentation and then trying to map this to the generated types and methods provided here. This project is about generating crates based on an API description and thus can't provide any support on how to use the generated crates, beyond addressing bugs and shortcomings of course. |
Beta Was this translation helpful? Give feedback.
-
I have decided to answer this question after struggling to send any email. use std::{fs, io::Cursor, path::PathBuf};
use google_gmail1::{
api::Message,
hyper::{self, client::HttpConnector},
hyper_rustls::{self, HttpsConnector},
oauth2::{self, authenticator_delegate::InstalledFlowDelegate},
Gmail,
};
use lettre::message::{header, Body, MultiPart, SinglePart};
use crate::{google_api::load_secret, models::AppError};
pub async fn send_email(
topic: &str,
content: &str,
gpc_path: PathBuf,
token_store_path: PathBuf,
flow_delegate: Box<dyn InstalledFlowDelegate>,
) -> Result<(), AppError> {
let gmail = create_gmail_service(gpc_path, token_store_path, flow_delegate).await?;
let pdf = fs::read("/path/to/sample.pdf")?;
let pdf_body = Body::new(pdf);
let email = lettre::Message::builder()
.from("[email protected]".parse().unwrap())
.to("[email protected]".parse().unwrap())
.subject(topic)
.multipart(
MultiPart::mixed()
.singlepart(
SinglePart::builder()
.header(header::ContentType::parse("text/plain; charset=utf8")?)
.body(content.to_string()),
)
.singlepart(
SinglePart::builder()
.header(header::ContentType::parse("application/pdf")?)
.header(header::ContentDisposition::attachment("output.pdf"))
.body(pdf_body),
),
)
.map_err(|e| AppError::Internal(e.to_string()))?;
let response = gmail
.users()
.messages_send(Message::default(), "me")
.upload_resumable(
Cursor::new(email.formatted()),
"message/rfc822".parse().expect("mime rfc822 is valid"),
)
.await?;
println!("response: {:?}", response);
Ok(())
} Technically gmail expects data in rfc822 format which can be provided in |
Beta Was this translation helpful? Give feedback.
Generally I recommend going by examples in the official documentation and then trying to map this to the generated types and methods provided here.
This project is about generating crates based on an API description and thus can't provide any support on how to use the generated crates, beyond addressing bugs and shortcomings of course.