[Solved]How to use String for email address

I am a beginner in rust, and I have a stupid question: how can I use a String variable to create an address to send an email with a letter?

// Dosen't work
let addr = "nomail@noserver.xxx";
let email = Message::builder().from(addr)

// Works fine
let email = Message::builder().from("nomail@noserver.xxx".parse().unwrap())

Thanks

In the second case, you're parsing the string into email address, but in the first one you try to use the string directly and probably get a type mismatch. Message::builder().from(addr.parse().unwrap()) should work.

Side note - it'd be easier to help if you formatted your code and specified the exact error you've got from cargo check - for now, my answer is just a guess.

1 Like
 let e1 = "nomail@noserver.com";
        let email = Message::builder()
            .from(e1)
            .to("nomail@noserver.com".parse().unwrap())
            .subject("New IP found")
            .body(body)
            .unwrap();
 .from(emailaddress)
    |              ---- ^^^^^^^^^^^^ expected struct `Mailbox`, found enum `Result`
    |              |
    |              arguments to this function are incorrect

Sorry, but the error refers to some other code (and this is only part of it) - there's no emailaddress here. Anyway, did you see the chapter on error handling in Rust book?

2 Likes

if use a variable e1 the program can not compile, if I put directly the string the program can be compiled

this code can be compiled

 let email = Message::builder()
            .from("nomail@noserver.com".parse().unwrap())
            .to("root@starfleet.local".parse().unwrap())
            .subject("New IP found")
            .body(body)
            .unwrap();

Do you mean that let email = Message::builder().from("nomail@noserver.com") works?

1 Like

Yes
This works

let email = Message::builder()
            .from("nomail@noserver.com".parse().unwrap())
            .to("root@starfleet.local".parse().unwrap())
            .subject("New IP found")
            .body(body)
            .unwrap();

Note that this code is not what I was asking about.

1 Like

But it has already been explained to you that this is not the case. Please read the previous posts carefully. It doesn't matter whether a value comes from a literal or a variable, all that matters for the compiler is its type. If

.from("nomail@noserver.com")

works, then substituting it with the variable e1 will also work. Either both work or neither.

works this

.from("nomail@noserver.com".parse().unwrap())

not

.from("nomail@noserver.com")

Yeah, that is exactly what we were saying. In this case, .from(e1.parse().unwrap()) will also work.

Ok but How can I use a String variable instead of "string".parse().unwrap() ?

Please re-read the code example in my second sentence in my previous answer.

surely then I had made another mistake when I made that substitution.
Many thanks

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.