(Ruby) Getting Net::Smtp Working with Gmail...

(Ruby) Getting Net::SMTP working with Gmail...?

I actually just got this working. Wrote a quick script to test it.

I was getting a different error than you were (requiring STARTTLS), I also found I had to use port 587 instead of 465.

I found the trick to get it working in a Rails plugin I found. (agilewebdevelopment.com/plugins/net_smtp_tls_support)

if you 'eval' this file (it adds tls support to the standard Net::SMTP library):

http://happiness-is-slavery.net/wp-content/rails-plugins/smtp_add_tls_support/lib/smtp_add_tls_support.rb

then run 'Net::SMTP.enable_tls()'

everything seems to work fine.

Here's my code:

require 'rubygems'
require 'net/smtp'

eval File.read("smtp_tls.rb")
Net::SMTP.enable_tls()
FROM_EMAIL = "REMOVED"
PASSWORD = "REMOVED"
TO_EMAIL = "REMOVED"

msgstr = <<END_OF_MESSAGE
From: Your Name <#{FROM_EMAIL}>
To: my phone <#{TO_EMAIL}>
Subject: text message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>

This is a test message.
END_OF_MESSAGE

Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
FROM_EMAIL, PASSWORD, :plain) do |smtp|
smtp.send_message msgstr, FROM_EMAIL, TO_EMAIL
end

obviously, i downloaded the above mentioned file to the same directory and named it 'smtp_tls.rb'

Hope this helps!

Ruby sending mail via gmail smtp

Note this answer is now out of date as per @EricDuminil

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. Which apparently means this answer no longer applies. Too bad.. –

If you're not using 2 Factor Authentication

Go to this link:

https://www.google.com/settings/security/lesssecureapps

And select:

"Access for less secure apps"

as per:

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

In this case you would use your normal email and password to connect.

If you are using 2 Factor Authentication

You need to create an app specific password for your application. Follow these steps:

  1. Go to gmail

  2. in the top right corner click on your profile icon and select 'My Account'

  3. Click on 'Sign in & Security'

  4. Scroll down the 'Sign in & Security' page a bit and there is a section called 'App Passwords' Click on that.

  5. You should see a dropdown labelled 'Select App'. Select Mail.

  6. For the 'on my device' dropdown select 'Other' and type in commandline or whatever you want to call the app.

  7. Click 'Generate'. A password will be generated. Copy that password and replace the password you were using in your options hash with the generated password:

    options = { :address => "smtp.gmail.com",
    :port => 587,
    :user_name => '',
    :password => '<generated_password_for_app>',
    :authentication => 'plain',
    :enable_starttls_auto => true }

That should be it. I just tried this and it worked for me.

Also make sure your username is your full gmail email address.

You can also find the 'Official docs' here: https://support.google.com/accounts/answer/185833?hl=en

NoMethodError (undefined method `timeout' for # Net::SMTP smtp.gmail.com:587 started=false ):

Ok so the issue was that i had the tlsmail gem installed at some point. This gem replaces the smtp classes used for sending out emails. I removed the gem and i am good to go.

Sending an e-mail with an attachment using ruby net/smtp

I've finally managed to send a binary attachment - the secret was using

Content-Type: application/octet-stream; name="#{filename}"
Content-Disposition: attachment; filename="#{filename}"; size=#{size}

in the attachment portion (part 3) of the message.

One other thing, this worked fine for a small test attachment, but when I tried a larger (140K) attachment, the attachment was truncated. Using

filecontent = File.binread(pathname)

rather than

filecontent = File.read(pathname)

seems to solve the problem. (I'm not quite sure why.)

Can't send mail with gmail smtp server (in discourse)

At last, I found the (stupid) reason.

I should start sidekiq in production mode:

nohup bundle exec sidekiq -e production > log/sidekiq.log 2>&1 &


Related Topics



Leave a reply



Submit