What's the Easiest Way to Send a Message Through Outlook with Ruby

What's the easiest way to send a message through Outlook with Ruby?

Assuming that the Outlook credentials are stored and you are set to autologin to Outlook, WIN32OLE does the trick quite nicely:

require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = "Hey look a subject!"
message.Body = "Yes this is dog"
message.Recipients.Add 'dog@dog.com'
message.Recipients.Add 'cat@dog.com'
message.Attachments.Add('C:\Path\To\File.txt')
#Want to save as a draft?
message.Save
#Want to send instead?
message.Send

This is in fact quite well documented in "Automating Outlook with Ruby: Saving Mail Messages To Files", as is automating the rest of windows with Ruby.

You may have an authorization issue, which, if it appears, can be solved using "Advanced Security for Outlook".

Sending formatted/html Outlook email with Ruby Win32ole

Pretty sure you need to use HTMLBody instead of Body as in:

email.HTMLBody = '<h3>Test body in HTML format</h3>'

Sending mail with ActionMailer and Outlook/Mandrillapp SMTP server

I got it working with and without Mandrill. I didn't get any emails until 7 days later, my inbox was flooded with all my test emails.

Seems there was a glitch with my DNS, TXT records for my email account, which had caused the delay.

Also tried without Mandrill, and the mails are getting sent properly. So, posting the settings for Outlook here. Might come handy for someone else.

  config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.live.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => 'noreply@example.com',
:password => 'password',
:domain => 'example.com',
:authentication => 'plain'
}

Note: For use in production, set raise_delivery_errors to false.

Action mailer - Sending an email through a contact form with Ruby on Rails

Gmail used to allow simply passing your valid email and password via a Rails app in order to send emails. But they killed that functionality off, and require that you configure an App Specific Password for this purpose:

https://support.google.com/accounts/answer/185833?hl=en

I think the change came in 2018 or so, I remember that our Rails app was humming along just fine and suddenly it stopped sending messages. At that time, it wasn't widely announced by Gmail that they had made this change, if announced at all, which caught us off guard.

Create your app specific password and use those credentials. See if that starts sending emails for you. Your code looks fine.

I've never been able to successfully use a Yahoo account with a Rails app by the way.



Related Topics



Leave a reply



Submit