Rails 2.3.5
This is on my local.
I have a simple model / view/ controller
#contact.rb
def deliver_contact
ContactMailer.deliver_contact(self)
end
#contacts_controller
def create
#contact = Contact.new(params[:contact])
respond_to do |wants|
if #contact.save
#contact.deliver_contact
#flash[:notice] = 'Contact was successfully created.'
wants.html { redirect_to('/thanks') }
else
wants.html { render :action => "new" }
end
end
The log says its going out.. i can do it in my console and it says its going out. But nothing actually is received in my inbox. What am I missing?
Update
Here is my development.rb:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => '25',
:domain => "website.com",
:authentication => :login,
:user_name => "snackmail#gmail.com",
:password => "aged-cheese"
}
The Create Log
Processing ContactsController#create (for 127.0.0.1 at 2010-11-28 16:12:49) [POST]
Parameters: {"commit"=>"PUNCH IT, CHEWY!", "action"=>"create", "authenticity_token"=>"3zayXGIOWeNLwb+jhx5cIxWgHqEJdv6iwj6I=", "contact"=>{"name"=>"bob marley", "message"=>"asdfasdf", "state_id"=>"Regarding an existing order", "email"=>"daniel#gmail.com"}, "controller"=>"contacts"}
Cache miss: Spree::Config ({})
Preference Load (0.3ms) SELECT * FROM "preferences" WHERE ("preferences".owner_id = 1 AND "preferences".owner_type = 'Configuration')
Configuration Load (0.1ms) SELECT * FROM "configurations" WHERE ("configurations"."id" = 1)
CACHE (0.0ms) SELECT * FROM "configurations" WHERE ("configurations"."id" = 1)
Cache write (will save 2.65ms): Spree::Config
Contact Create (0.8ms) INSERT INTO "contacts" ("name", "city", "zip", "created_at", "optin", "updated_at", "state_id", "message", "email") VALUES('bob marley', NULL, NULL, '2010-11-28 21:12:49', NULL, '2010-11-28 21:12:49', 'Regarding an existing order', 'asdfasdf', 'daniel.levine4#gmail.com')
Sent mail to daniel#gmail.com
Date: Sun, 28 Nov 2010 16:12:50 -0500
From: info#jersey.com
To: daniel#gmail.com
Subject: HOLY !# you got mail!
Mime-Version: 1.0
Content-Type: text/html; charset=utf-8
<strong>You have just received a dank crispy email.</strong>
<br />
<p>
Here are the details of the message:
</p>
<p>
<strong>Name:</strong>
bob marley
</p>
<p>
<strong>Email:</strong>
daniel#gmail.com
</p>
<p>
<strong>Subject:</strong>
Regarding an existing order
</p>
<p>
<strong>Message:</strong>
<br />
asdfasdf
</p>
Redirected to http://localhost:3000/thanks
Completed in 893ms (DB: 5) | 302 Found [http://localhost/contacts]
update:
Tried using the gmail tls plugin but it didn't work. tried moving the settings around to environment.rb to development.rb .
I am using spree, but if I put something in environment or development.rb in /config it overrides Spree's defaults. Alternatively, I can create the mail server from within Spree's admin, and with the right specs, it still doesn't budge.
If you are running your application in develop mode your email will not be sent but logged. To actually send an email in develop mode change config/environment.rb to something like:
Rails::Initializer.run do |config|
...
config.action_mailer.delivery_method = :sendmail # add this line
end
In config\environments\development.rb do you still have the following lines:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
If so, comment those, and add the following lines
to enable deliveries from development:
# To test if we can actually send mails!
config.action_mailer.raise_delivery_errors = true # for test
config.action_mailer.perform_deliveries = true
I found two items that solved this piece for me.
I was using Spree, so that I meant I had to configure the mail settings internally. Before I realized that I had gone with #lbz's request to install a plugin for TLS in Gmail sendings. It turns out, that that silently conflicted with the settings, so when I uninstalled it, it worked.
To get emails to be sent on your testing server, you must also comment this line :
# config.action_mailer.delivery_method = :test
Related
I upgraded my Rails app to 5.0.6 from 4.2.5. Now emails are not being sent by the Mail gem. I don't seem to be getting errors. I updated the Mail gem to the latest version with no luck. I am not sure what else to try.
I am running:
Rails 5.0.6
Ruby 2.3.0
Mail 2.7.0
controllers/send_email.rb
class SendEmail < ApplicationController
def initialize(to_address, from_address, email_pass, subject, body)
begin
options = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => from_address,
:password => email_pass,
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to to_address
from from_address
subject subject
body body
end
puts("\nSent message. From: #{from_address} To: #{to_address} \nMessage body: \n#{body}")
return true
rescue Exception => e
puts e.to_s
return false
end
end
end
Update:
I tried sending an email with Action Mailer as well. It says it sent in the console but it never gets delivered. I am using similar settings for Action Mailer as I am for the Mail gem.
config/environments/development.rb
config.action_mailer.smtp_settings = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => ENV['default_username'],
:password => ENV['default_password'],
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
I am running this from the console:
EmailMailer.sample_email(UserEmail.first)
Console output:
Rendering email_mailer/sample_email.html.erb within layouts/mailer
Rendered email_mailer/sample_email.html.erb within layouts/mailer (0.1ms)
Rendering email_mailer/sample_email.text.erb within layouts/mailer
Rendered email_mailer/sample_email.text.erb within layouts/mailer (0.0ms)
EmailMailer#sample_email: processed outbound mail in 9.4ms
Solution:
I have some code that sends an email when there is an unknown error that I need to look at. When I updated Rails that code got caught in a loop that sent a bunch of emails really fast. That caused my server provider to mark the email account as a spam account. I am not sure why my original code was not showing any errors but when I ran EmailMailer.sample_email(UserEmail.first).deliver_now it gave me an error message that helped me track it down.
Well, wild guessing here but try this:
EmailMailer.sample_email(UserEmail.first).deliver_now
If you had Rails version below 4.2.1 it was not necessary to call deliver on the mail object for it to be delivered. From that point on you can operate on the mail object before delivering it, which can be now using
.deliver_now or later, using .deliver_later, which goes along with the use of ActiveJob or other queueing library.
I am using ActionMailer to send mails for a 'Contact Us' form in my application.
I am using Mandrill app for sending my emails.These are my settings:
config/environments/development.rb
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.mandrillapp.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => 'SMTP username i.e Mandrill a/c username',
:password => 'API key for development',
:domain => 'example.com',
:authentication => 'plain'
}
config/environments/production.rb
I have removed the line
config.action_mailer.raise_delivery_errors = true
and changed the password for production - which is Mandrill app API key for production.
app/mailers/contactus_mailer.rb
class ContactusMailer < ActionMailer::Base
default :from => "noreply#example.com"
default :to => "help#example.com"
def new_message(message)
#message = message
mail(:subject => "[WebsiteName] #{message.name + " - " + message.email}")
end
end
Validity of the above accounts on custom domain - example.com
The above email accounts i.e noreply#example.com & help#example.com are provisioned and fully functional. The above accounts are setup at Outlook.com and I have also double-checked the MX records for my domain example.com and the domain settings are Active for my domain. As a proof, I can send/receive emails on both accounts from the accounts.
Development and Production environment Logs:
When I use the Contact Us form in both environments, ActionMailer reports no errors and redirects successfully to Home page.
Started POST "/contact" for 127.0.0.1 at 2013-08-18 12:35:37 +0530
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UNgMrA04yk4sIbqtXjlLvLvDINgrBT5eP0wMSRYNgPs=", "message"=>{"name"=>"Dummy name", "email"=>"abc#pqr.com", "content"=>"Random body"}, "commit"=>"Send Message"}
Rendered contactus_mailer/new_message.text.erb (0.5ms)
Sent mail to help#example.com (2679ms)
Date: Sun, 18 Aug 2013 12:35:38 +0530
From: noreply#example.com
To: help#example.com
Message-ID: <52107242dbf6c_12a7f3fd8b1835ad03979#Jatins-MacBook-Pro.local.mail>
Subject: [WebsiteName] Dummy name - abc#pqr.com
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Name: Dummy name
Email: abc#pqr.com
Body: Random body
Redirected to http://localhost:3000/
Completed 302 Found in 3841ms (ActiveRecord: 0.0ms)
Mandrill App API log for Production environment:
Full Request:
{
"from_email": null,
"from_name": null,
"async": false,
"key": "API key for production",
"raw_message": "Received: from example.com (unknown [23.20.245.109])\n\t(Authenticated sender: key_production#gmail.com)\n\tby ip-10-31-147-25 (Postfix) with ESMTPSA id 6811A151A064\n\tfor <help#example.com>; Sun, 18 Aug 2013 08:19:11 +0000 (UTC)\nDate: Sun, 18 Aug 2013 08:19:11 +0000\nFrom: noreply#example.com\nTo: help#example.com\nMessage-ID: <5210837f5ce24_26e56b87992f#5c11fd99-5533-4855-af78-40e02c939412.mail>\nSubject: [WebsiteName] Dummy name - abc#pqr.com\nMime-Version: 1.0\nContent-Type: text/plain;\n charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\nName: Dummy name\n\nEmail: abc#pqr.com\n\nBody: Random body",
"to": [
"help#example.com"
]
}
Full Response:
[
{
"email": "help#example.com",
"status": "rejected",
"_id": "9c9f88c588ee4f369437b8dd5d531c8c",
"reject_reason": "soft-bounce"
}
]
Mandrill App API log for development environment:
The Full Request for development env. is similar to the production environment. However, in development the response is different.
Full Response:
[
{
"email": "help#example.com",
"status": "sent",
"_id": "e67f31f893a84ecdb0ed2438e5741ce1",
"reject_reason": null
}
]
NOTE: I am not getting email on my account help#example.com in both development and production environments.
Queries:
Why am I getting rejected status and soft-bounce reject reason for production env., whereas for development it says sent status and no reject reason.
Why am I not receiving any mails in both the cases?
P.S.
Initially, I wasn't using Mandrill app and was using smtp.live.com as my SMTP server along with my no reply#example.com credentials, but that didn't work out.
Then I switched to Mandrill after some digging on Google.
It'd be equally good if someone can help with the Outlook mail setup. That way, Mandrill won't be required at all.
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.
Do you have
config.action_mailer.default_url_options = {
:host => 'YOUR_HOST_NAME_HERE'
}
defined in application.rb or production.rb? It should be set to your domain name. I've found that some servers will reject mail without an explicitly defined hostname.
I am using ruby 1.9.3p194 and Rails 3.2.3.
I am facing problem while sending emails whose template has a link in it. I have written following code:
invitation_mailer.rb
def event_invitation(user, event)
#user = user
#event = event
mail(:to => #user.email, :subject => "Invitation to participate in #{#event.name} event")
end
event_invitation.html.haml
Hello,
%br
%br
Your friend #{#event.user.full_name} has invited you to participate in #{#event.name} event. If you want to accept
this invitation, use the following link:
= link_to calendar_index_url, calendar_index_url
%br
%br
#{t('shared.team')}
user.rb
def xyz
...
InvitationMailer.event_invitation(self, event).deliver
end
If I remove the link line in the view, I am able to receive emails but not with the link inside the view. But the log shows that an email has been sent.
LOG
Sent mail to abhimanyu#gmail.com (6117ms)
Date: Fri, 02 Nov 2012 20:59:33 +0530
From: invitation#dev.tld
To: abhimanyu#gmail.com
Message-ID: <5093e6dd6a275_14f733fc536034cd444087#Abhimanyus-iMac.local.mail>
Subject: Invitation to participate in new event event
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello,
<br>
<br>
Your friend Abhimanyu Kumar has invited you to participate in new event event. If you want to accept
this invitation, use the following link:
http://localhost:3000/calendar
<br>
<br>
Dev Team
Any help to figure out the problem would be appreciated.
Thanks in advance.
Did you specify the ActionMailer default_url_options in your environment config file? Either in config/environments/development.rb or config/environments/production.rb (depending on the environment you're working on), make sure you include the following:
config.action_mailer.default_url_options = { :host => "example.com" }
See more info here: http://api.rubyonrails.org/classes/ActionMailer/Base.html#label-Generating+URLs
I'd like to play around with sending mail from Rails in a development environment. My message is getting rendered (I can see it in the terminal I'm running rails console in).
In config/development.rb I have (yes, the port is really 26):
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "mail.mydomain.com",
:port => 26,
:user_name => "me#mydomain.com",
:password => "removed",
:authentication => :login
}
I have in mailers/user_mailer:
class UserMailer < ActionMailer::Base
default from: "me#mydomain.com"
def send_an_email
mail(:to => "me#gmail.com", :subject => "Test from Rails")
end
end
I have a view that's getting rendered properly in views/user_mailer/send_an_email.text.erb. I'm calling UserMailer.send_an_email.deliver upon a page request.
Rails doesn't complain about my configuration. I see the message in the rails server console output. However, I never get an e-mail, and I don't see any error messages in the console output (as you can see above, raise_delivery_errors = true!.
Steve, by chance have you made sure you're calling the deliver method?
mail(:to => "me#gmail.com", :subject => "Test from Rails").deliver
I forgot to do that, and was quite confused by the lack of errors. :)
I have tried all kinds of configurations but still I can't send an email in my development environment from rails.
I installed mailutils to try this from the command line and it worked, I received the email (in spam of course): echo test | mail -s Subject user#example.com
Here's my config:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true # still no logs about emails
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true # I can't believe I have to add this option. Does it even exist? I found it on google.
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "some_user#gmail.com",
:password => "abc123",
}
And here's the code in the mailer:
class UserMailer < ActionMailer::Base
default :from => "root#ubuntu"
def test_email
Rails.logger.debug 'test_email'
mail(:to => 'user#example.com', :subject => "testing rails")
end
end
The controller:
class PagesController < ApplicationController
def home
UserMailer.test_email
end
end
development.log:
[2012-03-01 18:26:45.859] DEBUG [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] test_email
[2012-03-01 18:26:45.888] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Rendered user_mailer/test_email (1.6ms)
[2012-03-01 18:26:45.898] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Rendered pages/home.html.erb within layouts/application (1.1ms)
[2012-03-01 18:26:46.815] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Completed 200 OK in 455ms (Views: 112.4ms)
I also tried using the console:
root#ubuntu:/srv/www/myapp# rails c
Loading development environment (Rails 3.2.1)
irb(main):001:0> UserMailer.test_email
=> #<Mail::Message:32110400, Multipart: false, Headers: <To: user#example.com>, <Subject: testing rails>, <Mime-Version: 1.0>, <Content-Type: text/html>>
UserMailer.test_email
Just creates a Mail::Message object. To actually send an email you need to do
UserMailer.test_email.deliver
(or starting with rails 4.2 deliver_now / deliver_later)
Regarding logging errors:
Just figured out by tinkering that a bang method counterpart exists for deliver, which throws an exception. Can be pretty useful to check if you just misspelled your username or password, or you just have some misconfigured settings.
UserMailer.test_email.deliver!
An updated answer for Rails 4.2 would be:
UserMailer.test_email.deliver_now!
The exclamatory mark is to raise an exception if there are any errors.
If you suspect it's your settings, try taking out the :domain. It worked for me a while ago.( Net::SMTPAuthenticationError in rails 3.1.0.rc5 when connecting to gmail )
But I don't see a :body option in the mail function. Perhaps that's the issue. Try sending it from rails console and see what happens. http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail