The thing is, reply_to could be empty, in which case I don't want to include the .reply_to(reply_to) method in this chain, because it will lead to a panic. There must be a way, but I am such a beginner...
I tried that before, but: no method named 'reply_to' found for enum 'Result' in the current scope. It seems like that when the builder finishes, it is no longer a builder type...
It seems even after calling .from and .to, the object is already no longer a MessageBuilder, but a Message... Is it something in the lettre design that makes it very difficult to conditionally apply a method on their builder objects? I don't like to have to make a separate builder for every occasion, I am already splitting up between "plain" and "with attachment"...
That shouldn't be the case, based on the docs for lettre::message::MessageBuilder - only body, multipart or singlepart will change ti from a MessageBuilder into Result<Message, EmailError>.
Can you put together a complete example showing the error, that we can clone and build locally? The ideal is if I can run git clone to get your example code, then cargo build to see the failure. That way, we're more likely to be able to help, because we can see exactly what you code looks like
Actually building and making that work is rather challenging - it's not as simple as cargo build and observe the error you're seeing. Could you commit a branch that fails to compile, and explain what you're expecting to see?
At a guess, the following would work for you:
let mut email = Message::builder()
.from(env.smtp_from.parse().unwrap())
.to(to.parse().unwrap());
if Some(reply_to) = reply_to {
email = email.reply_to(reply_to);
}
email.header(if html {ContentType::TEXT_HTML} else {ContentType::TEXT_PLAIN})
.subject(hbs.render("subject", &line).unwrap())
.body(hbs.render("body", &line).unwrap())
.unwrap();
Yes, everybody says that should work and it doesn't. How about you just put that in the file and do cargo rel?
EDIT: Actually, one thing in your suggestion is different, you're not proposing email = email... but plain email... and if I put that in the match statement, it works! I don't think it's very beautiful, but I'll go with this.
Without changes, this fails to cargo build for me because it's got a huge pile of dependencies, and I don't have a C musl toolchain installed - can you give me a version without (at least) the psm dependency, since that fails for me.
Strange I thought, that the .pipe didn't work, you would really expect it to. Then it occurred to me: in my rush to do everything 'proper' I had added a semicolon before the closing curly brackets, which is why it returned ().
This is the solution I was hoping for, and I will use this in the future!