Reputation: 139
I have a couple of different mailers setup on my system. One is working fine and the other gives no errors but doesn't send out an email.
The mailer that works looks like this:
class UserMailer < ActionMailer::Base
default from: "[email protected]"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.password_reset.subject
#
def password_reset(user)
@user = user
mail to: user.email, subject: "Password Reset"
end
def confirm_registration(user)
@user = user
mail to: user.email, subject: "Confirm Registration"
end
end
and when sending a password_reset email I get the following in the log:
Started POST "/password_resets" for 127.0.0.1 at 2013-12-06 17:24:32 -0600
Processing by PasswordResetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jwGUtsyNRbCfBRFJm3uqcVZA4luHc05Gf0OBm4jyFpI=", "email"=>"[email protected]", "commit"=>"Reset Password"}
[1m[35mUser Load (0.8ms)[0m SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
[1m[36m (0.2ms)[0m [1mBEGIN[0m
[1m[35mSQL (2.7ms)[0m UPDATE "users" SET "password_reset_token" = $1, "updated_at" = $2 WHERE "users"."id" = 2 [["password_reset_token", "c851e80faeb3612c654c512857cfb29afc6a4665"], ["updated_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00]]
[1m[36m (5.8ms)[0m [1mCOMMIT[0m
[1m[35m (1.2ms)[0m BEGIN
[1m[36mSQL (1.5ms)[0m [1mUPDATE "users" SET "password_reset_sent_at" = $1, "updated_at" = $2 WHERE "users"."id" = 2[0m [["password_reset_sent_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00], ["updated_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00]]
[1m[35m (1.6ms)[0m COMMIT
Rendered user_mailer/password_reset.text.erb (2.3ms)
Sent mail to [email protected] (316.8ms)
Date: Fri, 06 Dec 2013 17:24:32 -0600
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Password Reset
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
To reset your password, click the URL below.
http://localhost:3000/password_resets/c851e80faeb3612c654c512857cfb29afc6a4665/edit
If you did not request your password to be reset, just ignore this email and
your password will continue to stay the same.
Redirected to http://localhost:3000/
Completed 302 Found in 380ms (ActiveRecord: 13.7ms)
Also, the view for the message is defined as password_reset.text.erb and is located in app/views/user_mailer and looks like this:
To reset your password, click the URL below.
<%= edit_password_reset_url(@user.password_reset_token) %>
If you did not request your password to be reset, just ignore this email and
your password will continue to stay the same.
The mailer that doesn't work looks like this:
class PoolMailer < ActionMailer::Base
default from: "[email protected]"
def send_pool_message(pool, subject, msg, allMembers)
@pool = pool
@msg = msg
email_list = Array.new
@pool.users.each do |user|
if allMembers
entries = Entry.where(user_id: user.id)
else
entries = Entry.where(pool_id: pool.id, user_id: user.id, survivorStatusIn:true)
end
if entries.any?
email_list << "#{user.name} <#{user.email}>"
end
end
subject_text = pool.name + ": " + subject
if mail to: email_list, subject: subject_text
puts "Successfully sent email"
return false
else
puts "Couldn't send email"
return true
end
end
end
And the log looks like the following:
Started POST "/pools/2/pool_messages" for 127.0.0.1 at 2013-12-06 17:50:54 -0600
Processing by PoolMessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jwGUtsyNRbCfBRFJm3uqcVZA4luHc05Gf0OBm4jyFpI=", "subject"=>"This is a test", "msg"=>"A test of the group pool message.", "allMembers"=>"false", "commit"=>"Send email", "pool_id"=>"2"}
[1m[36mPool Load (8.9ms)[0m [1mSELECT "pools".* FROM "pools" WHERE "pools"."id" = $1 LIMIT 1[0m [["id", "2"]]
[1m[35mUser Load (0.7ms)[0m SELECT "users".* FROM "users" INNER JOIN "pool_memberships" ON "users"."id" = "pool_memberships"."user_id" WHERE "pool_memberships"."pool_id" = $1 [["pool_id", 2]]
[1m[36m (0.4ms)[0m [1mSELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 2[0m
[1m[35m (0.3ms)[0m SELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 3
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 4[0m
[1m[35m (0.4ms)[0m SELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 5
Rendered pool_mailer/send_pool_message.text.erb (0.7ms)
Redirected to http://localhost:3000/pools/2
Completed 302 Found in 1527ms (ActiveRecord: 11.0ms)
And the view for this email is called send_pool_message.text.erb and is located in app/views/pool_mailer and looks like this:
<%= @msg %>
This is an automated email sent by the pool administer. Do not reply to this
email.
The first mailer sends out the email correctly and logs it as well. The second mailer neither sends an email or logs the email. I'm not sure where to look to correct the problem because I get absolutely zero error messages on any of this.
Any help pointing me in the right direction to fix this would be greatly appreciated.
Upvotes: 0
Views: 963
Reputation: 2171
You need to call .deliver to your mailer function
PoolMailer.send_pool_message(...).deliver
Otherwiser it just generates the email without delivering it
Upvotes: 2