Search Gmail by sender, subject, date, or attachment
See more GMail REST API Examples
Choose a Gmail search query, follow every result page, and fetch From and Subject headers for each match. Edit the query to use operators such as from:, subject:, after:, before:, has:attachment, and filename:. Quoted phrases and special characters are URL-encoded once.
Gmail search dates use midnight Pacific Standard Time. Use Unix timestamps in seconds for exact date boundaries in another timezone. Spam and Trash are excluded unless includeSpamTrash=true is added. Search requires gmail.readonly or a broader mail scope. Query searches are unavailable with gmail.metadata.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = chilkat::JsonObject::new();
success = token.load_file("gmail-token.json").is_ok();
if !success {
println!("Could not read gmail-token.json from the private working directory.");
return;
}
if (token.json_type_of("access_token") != 1) || (token.string_of_equals("access_token", "", true)) {
println!("An OAuth access token is required.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&token.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
let search_query = "from:sender@example.com has:attachment filename:pdf".to_string();
let base_url = chilkat::StringBuilder::new();
let _ = base_url.append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100&q=");
let _ = base_url.append(&http.url_encode(&search_query).unwrap_or_default());
let url = chilkat::StringBuilder::new();
let mut page_token = String::new();
let page = chilkat::JsonObject::new();
let mut total = 0;
let mut more = true;
while more {
let _ = url.set_string(&base_url.get_as_string().unwrap_or_default());
let _ = url.append("&pageToken=");
let _ = url.append(&http.url_encode(&page_token).unwrap_or_default());
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &resp).is_ok();
if resp.status_code() == 0 {
println!("Gmail did not return an HTTP response.");
return;
}
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Gmail HTTP {}", resp.status_code());
success = reply.load(&resp.body_str()).is_ok();
if success {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
success = reply.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a Gmail JSON response.");
return;
}
// Keep this page while individual message requests reuse reply.
success = reply.make_copy(&page).is_ok();
if !success {
println!("{}", reply.last_error_text());
return;
}
let mut i = 0;
while i < page.size_of_array("messages") {
page.set_i(i);
let message_id = page.string_of("messages[i].id").unwrap_or_default();
let _ = url.set_string("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
let _ = url.append(&http.url_encode(&message_id).unwrap_or_default());
let _ = url.append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject");
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &resp).is_ok();
// A message can disappear between the search and the metadata request.
if resp.status_code() != 404 {
if resp.status_code() == 0 {
println!("Gmail did not return an HTTP response.");
return;
}
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Gmail HTTP {}", resp.status_code());
if reply.load(&resp.body_str()).is_ok() {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
if reply.load(&resp.body_str()).is_err() {
println!("Expected a Gmail JSON response.");
return;
}
println!("Message: {}", message_id);
let mut h = 0;
while h < reply.size_of_array("payload.headers") {
reply.set_i(h);
println!("{}: {}", reply.string_of("payload.headers[i].name").unwrap_or_default(), reply.string_of("payload.headers[i].value").unwrap_or_default());
h = h + 1;
}
total = total + 1;
}
i = i + 1;
}
page_token = String::new();
if page.json_type_of("nextPageToken") == 1 {
page_token = page.string_of("nextPageToken").unwrap_or_default();
}
more = page_token != "";
}
println!("Matching messages read: {}", total);