I keep getting this annoying error when trying to send emails to addresses that have a plus sign in them. For example, when I try to send to something like [email protected], I get this error message:
Net::SMTPSyntaxError: "501 5.1.3 Bad recipient address syntax"
This is really frustrating because Gmail and other email providers support these plus signs for filtering emails. The Ruby SMTP library seems to be rejecting valid email addresses.
Here’s the stack trace I’m seeing:
/usr/local/lib/ruby/1.8/net/smtp.rb:679:in `validate_response'
/usr/local/lib/ruby/1.8/net/smtp.rb:652:in `check_ok'
/usr/local/lib/ruby/1.8/net/smtp.rb:634:in `recipient_to'
/usr/local/lib/ruby/1.8/net/smtp.rb:545:in `transmit_message'
/usr/local/lib/ruby/1.8/net/smtp.rb:544:in `each'
/usr/local/lib/ruby/1.8/net/smtp.rb:544:in `transmit_message'
/usr/local/lib/ruby/1.8/net/smtp.rb:471:in `send_message'
/usr/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:684:in `smtp_delivery'
Is there a way to fix this validation issue? Has anyone found a workaround for sending emails to addresses with plus signs using Ruby?
Been there with legacy Ruby setups causing email headaches. Updating Ruby or patching validation works, but you’ve got a deeper problem.
I’ve hit this same issue across multiple projects. Cleanest fix? Move email handling outside your Ruby app entirely. SMTP quirks and outdated libraries suck - automation platforms handle this way better.
Set up a simple webhook that sends email data to an external service. No more SMTP syntax errors, no more plus sign headaches or edge cases. The platform handles RFC compliance and delivery while your Ruby code stays clean.
Replace all that problematic SMTP code with one HTTP request. You’ll get better delivery rates, bounce handling, and zero email infrastructure maintenance.
I’ve done this multiple times - eliminates validation headaches completely. Check out Latenode for email automation - handles all RFC compliance automatically: https://latenode.com
The Problem:
You’re encountering a Net::SMTPSyntaxError: "501 5.1.3 Bad recipient address syntax" error when sending emails with plus signs in the recipient address (e.g., [email protected]) using a Ruby application with the outdated Net::SMTP library from Ruby 1.8 and ActionMailer 2.3.5. This error arises because these older versions have stricter email validation that doesn’t fully comply with modern email address standards (RFC 5322), which allow plus signs for filtering.
Understanding the “Why” (The Root Cause):
The root cause is the incompatibility between the email address validation in your outdated Ruby and ActionMailer versions and the modern email address specification (RFC 5322). Net::SMTP in Ruby 1.8 and ActionMailer 2.3.5 don’t correctly handle the plus sign (+) in email addresses, interpreting it as invalid syntax. Modern email clients and servers generally support plus addressing for filtering emails, while older libraries may not.
Step-by-Step Guide:
-
Upgrade Ruby and ActionMailer: The most robust and recommended solution is to upgrade your Ruby version to 2.0 or later and use a current version of ActionMailer. Newer versions implement more compliant email address validation, resolving the incompatibility with plus signs. This is the definitive long-term solution. Refer to the official Ruby and Rails upgrade guides for detailed instructions on updating your project.
-
Alternative: Use the mail gem: If upgrading is not immediately feasible, consider using the mail gem as a replacement for ActionMailer’s direct SMTP interaction. This gem is known for its better handling of RFC-compliant email addresses, including those with plus signs. You can integrate it with your existing SMTP settings without significant code restructuring.
-
Verification (for both options above): After upgrading or switching to the mail gem, thoroughly test your email sending functionality with addresses containing plus signs to confirm the issue is resolved.
Common Pitfalls & What to Check Next:
- SMTP Server Configuration: Even with updated libraries, ensure your SMTP server explicitly supports plus addressing. Some mail servers, despite RFC compliance, have stricter custom validation rules that might reject such addresses. Test sending directly to your SMTP server via
telnet to confirm.
- Email Address Escaping: While less likely with newer versions, it’s worth checking that the email address isn’t being inadvertently escaped or modified before being sent. Ensure the address is passed to the SMTP library exactly as intended (
[email protected]).
- Error Handling: Implement comprehensive error handling within your email sending logic to catch any unexpected failures or validation issues that might arise even after the upgrade or gem change. This improves the robustness of your application.
Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!
Had this exact problem a few months ago - drove me nuts. Switching to the Mail gem fixed it for me instead of using ActionMailer’s SMTP directly. Mail gem handles RFC-compliant addresses way better and doesn’t break on plus signs. You can set it up with your current SMTP settings without rewriting much code. Also check your SMTP server config - some mail servers have their own validation that’s stricter than actual email standards. Found out certain hosting providers configure their SMTP to reject plus signs even though they’re totally valid under RFC 5322.
Check if you’re escaping the email address properly before passing it to the SMTP library. Ruby 1.8’s Net::SMTP has stricter parsing and chokes on special characters even when they’re valid. I’ve hit this issue before - try wrapping the recipient address in angle brackets like [email protected]. Sometimes that helps the parser handle it.
Also verify your SMTP server actually supports plus addressing. Not all do, even though it’s RFC compliant. Quick test: telnet directly to your SMTP server and manually try the RCPT TO command with a plus sign address. See if the server accepts it outside of Ruby.
Yea I had this issue too! Try URL-encoding the plus sign, like change [email protected] to user%[email protected]. That worked for me when I was on an older Ruby version, hope it helps!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.