gmail api error on sending message #352
Answered
by
Byron
Niedzwiedzw
asked this question in
Q&A
-
Hi! when trying to send an email I'm getting an error as follows: Caused by:
0: sending email
1: Bad Request: {"error":{"code":400,"errors":[{"domain":"global","message":"Recipient address required","reason":"invalidArgument"}],"message":"Recipient address required","status":"INVALID_ARGUMENT"}} this is the code, I'd be glad if someone took a look! pub(crate) async fn send_email(email_request: EmailRequest) -> Result<()> {
let EmailRequest { to, body, subject } = email_request;
let auth = get_auth_on_behalf_of_me().await?;
let hub = Gmail::new(hyper_client(), auth);
let body = base64::encode_config(&body, base64::URL_SAFE);
let headers: Vec<MessagePartHeader> = std::iter::empty()
.chain(std::iter::once(MessagePartHeader {
name: Some("To".to_string()),
value: Some(to.to_string()),
}))
.chain(std::iter::once(MessagePartHeader {
name: Some("From".to_string()),
value: Some(Self::MY_EMAIL.to_string()),
}))
.chain(std::iter::once(MessagePartHeader {
name: Some("Subject".to_string()),
value: Some(subject.to_string()),
}))
.collect_vec();
info!("sending email");
const SCOPES: &[&str] = &[
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.metadata",
];
let message = Message {
payload: Some(MessagePart {
body: Some(MessagePartBody {
data: Some(body),
..Default::default()
}),
headers: Some(headers),
..Default::default()
}),
..Default::default()
};
let req_builder = hub.users().messages_send(message, "me");
let req_builder = SCOPES
.iter()
.fold(req_builder, |acc, next| acc.add_scope(next));
let mut buffer = tempfile::tempfile().wrap_err("creating a temporary file")?;
let mime_type = "message/rfc822"
.parse()
.map_err(|e| eyre::eyre!("bad mime type: {e:#?}"))?;
req_builder
.upload(&mut buffer, mime_type)
.await
.wrap_err("sending email")?;
Ok(())
} |
Beta Was this translation helpful? Give feedback.
Answered by
Byron
Aug 17, 2022
Replies: 1 comment 1 reply
-
My best guess is that unless |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Byron
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My best guess is that unless
"me"
is an email address, like "[email protected]" it won't work. Otherwise I recommend usingproxyman
to look at the request actually sent and compare that to typical requests as documented in thegmail
API docs (by Google).