I'm sending email from a rust program.
Email text is in a Postgresql database with encoding UTF8. I query database via postgres crate: https://crates.io/crates/postgres
I'm working on Windows. If I print the message seems correct:
But when I receive it on Thunderbird it display: "Questo =C3=A8 il"
If I read it from Gmail web interface it seems correct, but when I check "Show original message", it is wrong again.
I read messages with a query like this:
let query = "SELECT * FROM myschema.retrieve_messages($1, $2, $3, $4);";
for row in &db_client.query(query, &[&pg_working_date, &max_messages_per_run, &_force_retry, &run_id]).unwrap()
Then I loop on results, and put them in a Vec. I retrieve body with:
mail_body: row.get("mail_body")
If I print body of message with:
println!("Body is: {}", m.mail_body);
It seems ok.
Then I use lettre crate to send email https://crates.io/crates/lettre
I connect like this:
let mailer = SmtpTransport::relay(&smtp_server)
.unwrap()
.credentials(smpt_credentials)
.build();
And I ship message with this code:
let shipping_message = Message::builder()
.from("Hello <shippingaddress@shippngdomain.something>".parse().unwrap())
.to(m.email_address.parse().unwrap())
.subject(m.mail_subject)
.body(m.mail_body)
.unwrap();
And then the shipment:
match mailer.send(&shipping_message) {some other irrelevant code}
There is something wrong in code? What can I check?
When you call MessageBuilder::body
your body is encoded:
Automatically gets encoded with
7bit
,quoted-printable
orbase64
Content-Transfer-Encoding
, based on the most efficient and valid encoding for body.
If this behaviour is not desirable, you can use a MIME
body with SinglePart
instead.
I think this should do the trick for you:
let shipping_message = Message::builder()
.from("Hello <shippingaddress@shippngdomain.something>".parse().unwrap())
.to(m.email_address.parse().unwrap())
.subject(m.mail_subject)
.singlepart(SinglePart::plain(m.mail_body)) // plain UTF-8 encoding
.unwrap();