Can't Send Emails over VPS

So I've been sending emails out using the lettre crate and it's been working fine on my computer. However, when I try to send an email over VPS (Linode), the code compiles and runs without any errors to completion but doesn't send the email. Linode already lifted their blocks on outgoing ports 25, 465, 587 on mine so that's not the issue. -It's also not the firewall as I've disabled that during testing. Could someone help me figure out why the emails aren't getting sent? Below is the appropriate code and necessary dependencies. Linode offers a free 100$ credit so you could easily make an account with the promo code and test things out. -You'd also need to request to lift their blocks on ports 25, 465, 587 or the code will time out. Try the following code on your computer and you'll see it work, on Linode it won't. Replace email with the email sending the message, password with the associated password, and recipient_email with the email receiving the message:

use lettre_email::Email;
use lettre::smtp::authentication::Credentials;
use lettre::{SmtpClient, Transport};

pub fn mail(address:String, message:String)
{
    let creds = Credentials::new
    (
        "email".to_string(),
        "password".to_string()
    );

    let mut mailer = SmtpClient::new_simple("smtp.gmail.com")
        .unwrap()
        .credentials(creds)
        .transport();

    let email = Email::builder()
        .to(address)
        .from("email")
        .subject("Verification Code")
        .text(message)
        .build()
        .unwrap();

    mailer.send(email.into()).ok();
}

fn main()
{
    mail("recipient_email".to_string(), "Hi".to_string());
}
[dependencies]
lettre = "0.9"
lettre_email = "0.9"

Please if anyone has any ideas let me know - my website really can't function without email verification. Thanks!

What mail transfer agent are you using, and how have you configured it? You might want to try these instructions for setting up Postfix in a send-only configuration. (They are from Digital Ocean, but should work essentially the same on a Linode host running Debian or Ubuntu.)

I'm using exim4; it's possible I may have configured it incorrectly because I didn't know what I was doing. But just to confirm, you need a mail transfer agent with something like Linode to send out emails programmatically? -I wasn't sure about that because I never had to install a mail transfer agent on my laptop for lettre to work. (Do most personal computers automatically come with a mail transfer agent?)

I just rebuilt my Linode to start from scratch so that exim doesn't interfere at all. Then I followed all the instructions in setting up Postfix in a send-only configuration. My program still doesn't send any emails over the VPS.

If you're using lettre's SendmailTransport then it will go through whatever package provides the sendmail command, which is usually the MTA. You'll need to make sure sendmail is able to send mail to the internet, possibly through a smart host. Often the default configuration will only allow sending mail to local users. You might want to look in your MTA's logs to see if it is getting the mails from lettre, and if there are any errors sending them to remote servers.

1 Like

According to the code in the first post, they're using SmtpTransport by directly talking to remote server, not SendmailTransport.

Ah, I missed that.

Have you configured DNS and rDNS for your linode instance?

If that doesn't help, I would worry that your linode address has been used by spammers in the past and is on a block list. You could check common block lists like SBL to see if this is the case.

Yeah configured DNS and rDNS already. Honestly, I just gave up and launched the website without email verification. The website is globalfun.earth if you're interested.

Use an external email provider with a free tier instead. Sendgrid, mailgun, mailchimp etc etc. I think Sendgrid has an unofficial rust API.

https://docs.rs/sendgrid/0.15.0/sendgrid/

Thanks for the suggestion, but the free tier is quite restrictive. I wouldn't be able to scale up with this very well, I'd prefer to keep my costs minimal.