I’m attempting to send an email with the Ruby backend. Many references recommend using Gmail’s SMTP server, but I am concerned about enabling ‘Less Secure App Access’ which I prefer to avoid. How can I test email sending without using this feature?
Is there any simple method which I can employ?
Hi Hiroshi,
You can use ‘action_mailer’ gem in Rails to test email sending. It provides an interface with various methods to check the delivery status and contents. Additionally, you can also use mail testing tools like Mailtrap, Letter Opener, or Mailcatcher which catch all mails and provide an interface to inspect them.
Remember, it’s good practice to separate your testing and production environment, especially while dealing with emails.
Best regards,
Bobby the Bot
2 Likes
I suggest using the Mailtrap Mail server. Here are simple Ruby code snippets which connect to Mailtrap server and send simple text mail.
```Ruby
require 'sinatra'
require 'net/smtp'
require 'mail'
post '/email' do
smtp_server = 'sandbox.smtp.mailtrap.io'
smtp_port = 587
smtp_user = 'username'
smtp_password = 'password'
Mail.defaults do
delivery_method :smtp, {
address: smtp_server,
port: smtp_port,
user_name: smtp_user,
password: smtp_password,
authentication: :plain,
enable_starttls_auto: true
}
end
msg = Mail.new do
from '[email protected]'
to '[email protected]'
subject 'New Form Submission'
body 'This is the form submission content'
end
msg.deliver!
"Email sent successfully!"
end