Sending smtp email via mail gem results in 554 5.7.0 Reject

3k views Asked by At

I'm trying to send emails from a rails application using smtp. Sending the emails does not cause problems, but some recipients do not receive the emails. Instead, delivery notices ("Undelivered Mail Returned to Sender") are returned with:

Diagnostic-Code: smtp; 554 5.7.0 Reject, id=04635-09 - spam: This message was
   blocked for security reasons

Now the strange part:

  • Sending a test mail through a mail client via smtp (TLS/SSL true, port 465, auth: password) works. The recipient receives the email without error.
  • Sending the email via sendemail -o tls=yes -f [email protected] -t [email protected] -s 212-227-10-158.benjaminkant.de -xu [email protected] -xp password -u "Hello from sendemail" -m "This is a test." -vvvv works. The recipient receives the email without error.
  • Sending the email through the mail gem (which is what I want to achieve) using the same smtp account works, but the recipient does not receive the email. Instead, "Undelivered Mail Returned to Sender" is sent to the sender address.

    require 'mail'
    
    options = {address: "212-227-10-158.benjaminkant.de", user_name: "[email protected]",
        password: "...", authentication: "login"}
    
    Mail.defaults { delivery_method :smtp, options }
    
    message = Mail.new(to: "[email protected]", from: "[email protected]",
        subject: "Test", body: "This is a test.")
    message.deliver
    

    Also, I've tried these options, resulting in the same issue:

    options = {address: "212-227-10-158.benjaminkant.de", user_name: "[email protected]",
        password: "...", authentication: "login"}
    
    options = {address: "212-227-10-158.benjaminkant.de", user_name: "[email protected]",
        password: "...", authentication: "login",
        enable_starttls_auto: true}
    
    options = {address: "212-227-10-158.benjaminkant.de", user_name: "[email protected]",
        password: "...", authentication: "login",
        port: 465, enable_starttls_auto: false, ssl: true}
    
    options = {address: "212-227-10-158.benjaminkant.de", user_name: "[email protected]",
        password: "..."}
    

Does this make sense to anyone?

Additional information

MX records

▶ host -t MX fiedlschuster.de
fiedlschuster.de mail is handled by 10 mail.fiedlschuster.de.

▶ host -t MX 212-227-10-158.benjaminkant.de
212-227-10-158.benjaminkant.de has no MX record
1

There are 1 answers

1
user208769 On

Firstly, it's worth checking message.to_s

Date: Wed, 24 Jul 2019 17:15:08 +0100
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Test
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
 Content-Transfer-Encoding: 7bit

This is a test.

The only thing that screams out at me is the Message-ID - it uses your machine name. It's possible that some spam filters compare this to the sender's domain name, and if they don't match, assume that you are a bot?

(I don't know what sendemail does here, but my Thunderbird certainly uses my domain name)

So I'd try:

message = Mail.new(to: "[email protected]", from: "[email protected]",
    subject: "Test", body: "This is a test.", message_id: "#{Mail.random_tag}@fiedlschuster.de")