how send mail with localhost using rails 4? - ruby-on-rails

I search the equivalent to very simple PHP mail function, in ruby.
I watch http://guides.rubyonrails.org/action_mailer_basics.html and I trid to use it but it's not clear.
The only exemple I found on guide and on others responses on SO it's about "gmail smtp".
I don't want used gmail smtp, just local smtp server to test mail function like on WAMP.
I tried :sendmail delivery_method, but it respond "/bin/sendmail doesn't exist", so I install postfix in local and add this config to development.rb :
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
log of my rails server :
UserMailer#welcome_email: processed outbound mail in 2.4ms
Sent mail to test#mytruedomain.com (59.5ms) Date: Wed, 10 Dec 2014
12:08:02 +0100 From: contact#test.com To: mytruedomain.com
Message-ID: <5488299262e_17a43f9c2ba19314937e6#matrix-PC.mail>
Subject: test Mime-Version: 1.0 Content-Type: text/html;
charset=UTF-8 Content-Transfer-Encoding: 7bit
un test
but I never recieve it in my mail box at test#mytruedomain.com...
EDIT: when I watch log of postfix :
postfix/error[6335]: 7ECBD5C29E5: to=, relay=none, delay=0.14, delays=0.08/0/0/0.06, dsn=5.0.0, status=bounced (mytruedomain.com)
What specific config is missing/needed with my local postfix?
So how can I do to send mail? I just want send mail, I don't understand how it's so complicated !

First test if your server is working:
date | mail -s test your.name#domain.com
Then configure your application to send the email using smtp:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "domain.com",
}

Try the code from Paulo Fidalgo, and I'll recommend you use mailcatcher.
Actually you just need this line in your development.rb file:
config.action_mailer.delivery_method = :smtp

Related

Sendgrid not sending email?

I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:
UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******#gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******#gmail.com
To: *******#gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0#albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.
This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.
However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.
Edit:
I tried doing it on production and it is not working. Here is more info:
My production.rb looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:
UserMailer.send_sign_up_email(#current_user).deliver
My mailer class looks like:
def send_sign_up_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! #{#user.first_name}")
end
However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?
I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones
# Don't care if the mailer can't send.
config.action_mailer.perform_caching = false
# Reference: http://stackoverflow.com/a/20770131/936494
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Then create an initializer /config/initializers/mail_config.rb and add following code to it:
TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)
# =========== DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
when :production, :staging, :experimental
:sendmail
when :test
:test
else
:smtp
end
ActionMailer::Base.delivery_method = delivery_method
# =========== SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)
gmail_config = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV['MAIL_USER_NAME'],
password: ENV['MAIL_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
if :smtp == delivery_method
use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)
smtp_settings = ( use_gmail_config ? gmail_config : {} )
ActionMailer::Base.smtp_settings = smtp_settings
end
# =========== DEFAULT URL OPTIONS
default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol
default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?
ActionMailer::Base.default_url_options = default_url_options
The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.
My /config/settings.yml looks like
default_url_options:
host: ''
port: ''
protocol: ''
and my /config/settings/development.yml looks like
default_url_options:
host: 'localhost'
port: '3000'
Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.
First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.

How to send localhost email and capture it using Thunderbird?

I'm trying to send an email from rails and capture it using thunderbird.
This is my mailer:
def hello_world
from = 'bruno#localhost'
subject = 'Hello World'
to = 'bruno#localhost'
main to: to, from: from, subject: subject
end
I followed this instructions to configure thunderbird locally and it is working fine.
https://gist.github.com/raelgc/6031274
I'm not sure how to config rails.
What should I put here in development.rb ?
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "localhost"
}
As #aniket-rao suggested: Use Mailcatcher which does all this. No need to install a local postfix server and fiddle with inboxes. From the docs:
MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that's arrived so far.
In your Rails environment use this configuration to send email to Mailcatcher:
# use Mailcatcher
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }
Your email will be visible on the local web app running on port 1080.
Try this
mail(to: to, from: from, subject: subject)

Rails 4 - yandex mail is not sending

In rails 4.2.4, I am using gem 'rails_config' for emails feature. In development environment all mails are sending properly but in test environment it is showing an error like Net::SMTPFatalError (553 5.7.1 Sender address rejected: not owned by auth user.):
In setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.yandex.ru",
:port => 465,
:domain => "yandex.ru",
:authentication => :login,
:user_name => "ssstest#yandex.com",
:password => "pwd567#",
:ssl=> true,
:enable_starttls_auto=> true,
:tls=> true
}
In user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "ssstest#yandex.com"
...
end
Server log's output is,
D, [2016-05-06T06:24:25.089270 #23566] DEBUG -- :
Devise::Mailer#confirmation_instructions: processed outbound mail in 954.3ms
I, [2016-05-06T06:24:26.500832 #23566] INFO -- :
Sent mail to sample_demo#yopmail.com (1408.8ms)
D, [2016-05-06T06:24:26.501240 #23566] DEBUG -- : Date: Fri, 06 May 2016 06:24:25 +0000
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: sample_demo#yopmail.com
Message-ID: <572c389917f09_5c0e10bb99411990#ip-10-65-178-51.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
How can I fix this issue? Please help me
As the error log indicates, please change the from email address inside config/initializers/devise.rb
config.mailer_sender = 'ssstest#yandex.com'
port: 465
authentication: login
smtp_enable_starttls_auto: true
smtp_openssl_verify_mode: true
As mentioned here
https://docs.gitlab.com/omnibus/settings/smtp.html#yandex
Have you added config.action_mailer.delivery_method = :smtp in the environments/test.rb ? And it is not a good practice to store usernames and passwords right in the email setup. Store them as environment variables.

EOFError error trying to use Amazon SES via SMTP with Rails 3.1.3

I have a Rails app configured to use Amazon SES via SMTP. When I try and send email, though, it appears to timeout after a minute, and I get an EOFError. It smells like a configuration issue--the email seems to be constructed fine, and I can send myself test emails from the AWS SES console. This is in sandbox mode, and the app is running in development mode, but both the sending and receiving emails have been verified with SES, and development.rb is set up with this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
I've tried a million config variations; this is starting to drive me bananas. Any help or guidance would be very, very appreciated. More details:
The smtp config, which I have in an initializer:
ActionMailer::Base.smtp_settings = {
:address => "email-smtp.us-east-1.amazonaws.com",
:port => "465",
:authentication => :plain,
:enable_starttls_auto => true,
:user_name => "1234",
:password => "abcde"
}
The logs with the error, a bit truncated for brevity:
Sent mail to john#phu.com (59929ms)
Date: Tue, 20 Dec 2011 03:08:37 -0800
From: contact#phu.com
To: john#phu.com
Message-ID: <4ef06cb5ed3c_d73c3fc604c34d4491943#Johns-MacBook-Pro.local.mail>
Subject: Your invitation to Phu
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
....
Completed 500 Internal Server Error in 60564ms
EOFError (end of file reached):
app/controllers/admin_controller.rb:61:in `block in send_invite'
app/controllers/admin_controller.rb:46:in `send_invite'
There is also a solution without the monkey-patch solution from Mihir (which, according to AWS SES documentation, http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/SMTP.Connecting.html, is the TLS wrapper solution), by using port 587 and :enable_starttls_auto option (the STARTTLS solution). So the modified config is such:
config.action_mailer.default_url_options = { host: “<example.com>” }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address: “email-smtp.us-east-1.amazonaws.com”,
:port: 587,
:domain: “<example.com>”,
:authentication: :login,
:user_name: “<your aws smtp username>”,
:password: “<your aws smtp password>”,
:enable_starttls_auto => true
}
Here is a solution in case you want to use SMTP (and not the AWS-SES gem)
http://blog.readypulse.com/2012/01/06/amazon-ses-smtp-emails-using-rails-3-1-in-three-easy-steps/
Things to note
AWS SMTP only works on TLS
AWS SMTP does NOT support STARTTLS
ActionMailer’s configuration does not have an easy TLS switch.
Steps to follow
Enable SMTP support on your AWS Console – See instructions here.
Create an initializer under your config/initializers directory. I am calling it amazon_ses.rb and add following code to money patch ActionMailer’s SMTP settings.
module Net
class SMTP
def tls?
true
end
end
end
Add following code in your development.rb and production.rb files. Modify the settings to match your environment.
config.action_mailer.default_url_options = { host: “<example.com>” }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: “email-smtp.us-east-1.amazonaws.com”,
port: 465,
domain: “<example.com>”,
authentication: :login,
user_name: “<your aws smtp username>”,
password: “<your aws smtp password>”
}
I ran into this same issue using Rails 2.3, with Ruby 1.8.7, in dev mode with a sandboxed SES account, sending to/from verified senders.
I worked around it by adding in the unofficial aws-ses gem. Stick it in your Gemfile, and then replace the smtp settings with these 5 lines:
# Configure mail sending options: Use AWS-SES for all environments
config.after_initialize do
ActionMailer::Base.delivery_method = :amazon_ses
ActionMailer::Base.custom_amazon_ses_mailer = AWS::SES::Base.new(:secret_access_key => 'asfd/1234', :access_key_id => 'ABCD')
end
Sending then worked as expected…which tells me the emails themselves were getting set up correctly.
I've done a lot of googling, and haven't seen any confirmation that SES-SMTP is compatible with Rails 2.3 + Ruby 1.8.7. Nor have I found anything that explicitly denies it either, beyond your & my experience.
Let us know if you find a solution!
SES requires a SSL session before the EHLO command is sent.
I know System.Net.Mail doesn't work with SES, because System.Net.Mail initiates the TLS after the SMTP session has started.
I got this working on Rails 3.2.12 adding require 'net/smtp' to the initializer file with the module change like:
require 'net/smtp'
module Net
class SMTP
def tls?
true
end
end
end

No errors in development.log, but mail wouldn't be sent

I use ActionMailer to send my email through Rails, but I can't receive anything.
My development.log said:
Sent mail to mymail#gmail.com (2107ms)
Date: Wed, 07 Dec 2011 17:14:30 +0800
From: no-reply
To: mymail#gmail.com
Message-ID: <4edf2e765a139_147105390c874e3#raincole-laptop.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>
Hello mymail#gmail.com!
</p>
<p>Someone has requested a link to change your password, and you can do this through the link below.</p>
<p>Change my password</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
But there is nothing new in my inbox or spam box.
I have set:
ruby-1.9.2-p290 :008 > Rails.application.config.action_mailer[:raise_delivery_errors]
=> true
So, how can I see the real error logs to debug?
Edit:
I write a email.yml:
development:
reply: "no-reply"
host: "starfish.dev"
smtp:
address: "smtp.gmail.com"
port: 587
domain: "starfish.dev"
authentication: "plain"
user_name: "mymail#gmail.com"
password: "mypassword"
enable_starttls_auto: true
Then in application.rb:
$EMAIL_CONFIG = ActiveSupport::HashWithIndifferentAccess.new YAML.load(File.open("#{Rails.root}/config/email.yml"))[Rails.env]
config.action_mailer.smtp_settings = $EMAIL_CONFIG[:smtp].symbolize_keys if !$EMAIL_CONFIG[:smtp].nil?
in development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
But, it still just sends to log file, no to real address.
The development and test environments don't use real mailers (unless you specifically configure them). By default it just does exactly what you have noticed: it goes through all the motions of sending an email... but then doesn't actually send it, just logs the email that would have been sent.
EDIT: note that this is a reply to the original question, which did not contain any smtp settings.
have you tried telnet smtp.yoursite.com 587?
if there's no responding, please check your firewall.

Resources