So I am testing out the Ruby gem 'Mail'. Below is my code. According to the sysadmin, there is no authentication necessary to send out by SMTP and the server is allowed to send by SMTP. If that makes sense.
I have used the 'net/smtp' which worked fine but I can't seem to get this 'Mail' gem to work for me. It's not giving me any errors so I'm not sure what the issue is. I am a complete ruby noob at this.
#testing out mailer
require 'mail'
Mail.defaults do
delivery_method :smtp, {
:address => '10.18.34.18',
:port => '25',
:user_name => 'anonymous',
:password => 'anonymous',
:authentication => 'plain',
:enable_starttls_auto => true}
end
mail = Mail.new do
from 'server#company.com'
to 'cvu#company.com'
subject 'This is a test email'
body File.read('test.txt')
end
mail.to_s
Try calling mail.deliver!, calling to_s just returns you a string representation of the object.
Alternatively you can call deliver with a block instead of calling new, e.g.
mail = Mail.deliver do
from 'server#company.com'
to 'cvu#company.com'
subject 'This is a test email'
body File.read('test.txt')
end
In addition to what struthersneil stated, I had to remove the authentication like so:
require 'mail'
Mail.defaults do
delivery_method :smtp, {
:address => '10.18.34.18',
:port => '25',
:enable_starttls_auto => true}
end
Related
I'm trying to send a simple email using my Gmail account through ruby's mail gem but the email is never sent nor received. I followed the steps from a similar question but I wasn't sure what to put under the domain field. I believe this may be my issue but I'm not sure.
my code:
require 'mail'
options = { :address => "smtp.gmail.com",
:port => 587,
:domain => 'your.host.name',
:user_name => 'REMOVED',
:password => 'REMOVED',
:authentication => 'plain',
:enable_starttls_auto => true }
Mail.defaults do
delivery_method :smtp, options
end
mail = Mail.new do
from 'REMOVED'
to 'REMOVED'
subject 'This is a test email'
body 'test'
end
I don't see where you are calling the deliver method to send the email. Based on the documentation, there's a couple of ways to do that:
Mail.deliver do
from 'me#test.lindsaar.net'
to 'you#test.lindsaar.net'
...
end
or
mail = Mail.new do
from 'me#test.lindsaar.net'
to 'you#test.lindsaar.net'
...
end
mail.deliver!
Is there a way to receive emails via IMAP or SMTP through the 'mail' gem?
The documentation only seems to mention POP3:
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username>',
:password => '<password>',
:enable_ssl => true
end
The documentation actually talks directly about SMTP.
Mail.defaults do
delivery_method :smtp, address: "localhost", port: 1025
end
That is how you default to sending via SMTP.
To receive, instead of using delivery_method use retriever_method
IMAP works by simply adding this:
Mail.defaults do
retriever_method :imap, :address => 'imap.gmail.com',
:port => 993,
:user_name => '<username>',
:password => '<password>'
:enable_ssl => true
end
I'm still not able to get SMTP retrieval to work..
This is a newbie question so please excuse me, I have been working with rails, but this is the first time i am trying to require gems from a heroku app that does not include rails - just a plain Ruby app.
ok I have a app.rb file looking like this:
require "sinatra"
require 'koala'
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587,
:domain => "MYDOMAIN",
:authentication => :plain,
:user_name => "USER_NAME",
:password => "PASSWORD",
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths= File.dirname(__FILE__)
class TestMailer < ActionMailer::Base
default :from => "MY_EMAIL"
def welcome_email
mail(:to => "MY_EMAIL", :subject => "Test mail", :body => "Test mail body")
end
end
What I would like to do is run
TestMailer::deliver_test_email
in the console and get the details or run
TestMailer::deliver_test_email.deliver
to send a test email
but all i get is :
NameError: uninitialized constant Object::TestMailer
I have included actionmailer in the Gemfile and its also in the Gemfile.lock
I am sure it is something straight forward for an experienced Ruby dev, but I am struggling could anyone help me please?
Thanks
After a few hours of testing and research I ended up using Mail instead, this was my final code which when run works fine, I hope it helps anyone who is having the same issues as I was :)
require 'mail'
Mail.defaults do
delivery_method :smtp, { :address => "smtp.sendgrid.net",
:port => 587,
:domain => "MY_DOMAIN",
:user_name => "USER_NAME",
:password => "PASSWORD",
:authentication => 'plain',
:enable_starttls_auto => true }
end
mail = Mail.deliver do
to 'EMAIL'
from 'Your Name <NAME#MY_DOMAIN>'
subject 'This is the subject of your email'
text_part do
body 'hello world in text'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<b>hello world in HTML</b>'
end
end
I've got an entry form that asks for a persons name and email address. I save that email address to the session so I can access it after the form has been submitted. Then I use Pony to send a thank you/notification email to the person who submitted the form. However, while it sends without issue to a MobileMe address, it won't to a gmail address. The line I'm using to send is:
Pony.mail(:to => "#{#email}", :from => 'from#email.com', :subject => "Thanks for entering!",
:body => "Thank you!")
The #email variable is defined in the handler and gets the value from the session.
Any ideas?
Here's the helper method I use that uses Pony to send email using sendmail when in development on my Mac or via sendgrid on Heroku when in production. This works reliably and all of my test emails get sent to my various gmail addresses.
Possibly your problem is that your from address is invalid and Google is flagging that as spam. Also I note you are not setting the Content-Type header, which is typically text/html in my case.
def send_email(a_to_address, a_from_address , a_subject, a_type, a_message)
begin
case settings.environment
when :development # assumed to be on your local machine
Pony.mail :to => a_to_address, :via =>:sendmail,
:from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message
when :production # assumed to be Heroku
Pony.mail :to => a_to_address, :from => a_from_address, :subject => a_subject,
:headers => { 'Content-Type' => a_type }, :body => a_message, :via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => 25,
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN'] }
when :test
# don't send any email but log a message instead.
logger.debug "TESTING: Email would now be sent to #{to} from #{from} with subject #{subject}."
end
rescue StandardError => error
logger.error "Error sending email: #{error.message}"
end
end
Pony is broken in ruby 1.9. Does any recommend any alternatives for me so I can send mail through my Gmail account? Cheers
Pony works for me in Ruby 1.9, and I'm also using the gmail SMTP server. Here's how I have it set up...
require 'pony'
Pony.mail(:to => 'real_mail#yahoo.com', :via => :smtp, :via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'id_gmail',
:password => 'parola_gmail',
:authentication => :plain,
:domain => "HELO",
},
:subject => 'Test email', :body => 'Test for Pony email through gmail SMTP server.')
Have you tried Mail from Mikel Lindsaar? It's got really great syntax