Ruby Contact Mailer not working properly - ruby-on-rails

I have a simple script for my contact_mailer.rb which has a simple form as the front end with one dropdown to select purpose and has a switch-case block to choose the email ID accordingly. Now, somehow mail is always being sent to the first one i.e. when 'localization'.
Only this gets fired whatever be selected. Please explain why this may be happening. Am pasting code snippet below for reference:
class ContactUsMailer < ActionMailer::Base
default :from => "bot#mydomain.com"
def contact_us_email(name, message, purpose, email)
#name = name
#message = message
#purpose = purpose
#email = email
content_type "text/html"
case #purpose
when 'localization'
mail(:to => 'me#mydomain.com', :subject => purpose)
when 'marketing'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com, me#mydomain.com', :subject => purpose)
when 'network'
mail(:to => 'me.agm#gmail.com', :subject => purpose)
when 'recruitment'
mail(:to => 'me#mydomain.com', :cc => 'me#mydomain.com', :subject => purpose)
when 'general'
mail(:to => 'me#mydomain.com', :subject => purpose)
else
mail(:to => 'me#mydomain.com', :subject => purpose)
end
end
end
Thanks and Regards

Related

Rails actionmailer: attachments from database

I'm using actionmailer to send mails in rails. I want to attach multiple attachments:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
attachments = self.prepare_attachments(languages)
mail(
:to => recipient,
:subject => 'Test'
)
end
The delivered mail doesn't contain any attachment.
This is working:
def distribution_email(recipient, languages)
attachments['test.pdf'] = File.read("/tmp/test.pdf")
mail(
:to => recipient,
:subject => 'Welcome to My Awesome Site'
)
end
What am i doing wrong?
I fount the solution, one must not override attachment:
def prepare_attachments(languages)
attachments = {}
languages.each do |language|
next unless language.document
attachments[language.document.filename] = language.document.read
end
return attachments
end
def distribution_email(recipient, languages)
self.prepare_attachments(languages).each do |filename, content|
attachments[filename] = content
end
mail(
:to => recipient,
:subject => 'Test'
)
end

Rails: change default sender in action mailer

I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:
class UserMailer < ActionMailer::Base
default :from => "example#example.com"
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello")
end
end
I tried this:
class UserMailer < ActionMailer::Base
def welcome_email(user, order)
#user = user
#order = order
mail(:to => user.email, :subject => "Your Order", :from => "abc#xyz.com")
end
def signup_email(user)
#user = user
mail(:to => user.email, :subject => "Thank you.", :from => "qwe#asd.com")
end
def invite_confirm(curuser,usemail,post)
#greeting = "Hi"
#user = curuser
#post = post
mail(:to => user.email, :subject => "Hello", :from => "zyx#asd.com")
end
end
But still it is sending email from "example#example.com"
Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?
In config/environments/development.rb and config/environments/production.rb i have this:
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "example#example.com",
:password => "example",
:enable_starttls_auto => true
}
I guess, i should not change anything here.
You can pass it as a parameter to the mail method:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end
I think you want to send mail with three different emails of the for-each action. Because you use gmail, you need Sending mail from a different address.
No single vendor is optimal for all three types of email; you likely
will use several vendors.
For “company email,” that is, sending individual email to customers or
business associates, you’ll probably use Gmail or Google Apps for
Business. For a single address, you can set up a single Gmail account
to receive and send email from a different address. More likely,
you’ll want several email addresses for your company mail. For that,
use Google Apps for Business.
Send Email with Rails
I found that, this can't be done using smtp. Need to use amazon SES which allows multi sender support.
Here's what i use, it allows to make a "title" different.
class UserMailer < ActionMailer::Base
default :from => '"example" <example#domain.com>'
def send_signup_email(user)
#user = user
mail(to: #user.email, subject: 'example')
end
end

Sending two different email with one common attachment, get merged together?

I'm sending out two different e-mails:
mail(:to => email1, :template_name => "mail1_template",
:subject => "Mail 1").deliver!
mail(:to => email2, :template_name => "mail2_template",
:subject => "Mail 2").deliver!
It works fine up until the point when I add an attachment:
attachments["file.pdf"] = File.read("file.pdf")
mail(:to => email1, :template_name => "mail1_template",
:subject => "Mail 1").deliver!
mail(:to => email2, :template_name => "mail2_template",
:subject => "Mail 2").deliver!
There's nothing wrong with the PDF attachment itself, but, the message that is being received by the email2 recipient, for some non-obvious reason, is being merged with the first one sent to the "email1" recipient: The "email2" recipient receives both email contents in a single email.
Once I remove the attachment line, everything gets back to normal.
How would I fix it?
Try to converse the mails meaning first send email2 and then send email1 -
attachments["file.pdf"] = File.read("file.pdf")
mail(:to => email2, :template_name => "mail2_template",
:subject => "Mail 2").deliver!
mail(:to => email1, :template_name => "mail1_template",
:subject => "Mail 1").deliver!
The reason to do so is to let know that now which email gets both merged mails.
The debugging is helpful too.
Assuming that its your development environment, so don't forget to put the line in your /config/environments/development.rb:
config.action_mailer.raise_delivery_errors = true
And check the logs from development.log to trace where the error actually is.
UPDATE:
Also, you have sent the attachment above the 2 mails. The ruby gets confused as to whom you are going to send attachment i.e. the first email or the other or both. Do one thing- put attachment in between of the two mails and then check the result.
mail(:to => email2, :template_name => "mail2_template",
:subject => "Mail 2").deliver!
attachments["file.pdf"] = File.read("file.pdf")
mail(:to => email1, :template_name => "mail1_template",
:subject => "Mail 1").deliver!
UPDATE:
In order to send attachments to both emails:
attachments["file.pdf"] = File.read("file.pdf")
mail(:to => email2, :template_name => "mail2_template",
:subject => "Mail 2").deliver!
attachments["file.pdf"] = File.read("file.pdf")
mail(:to => email1, :template_name => "mail1_template",
:subject => "Mail 1").deliver!
Hope it will help you.

RSPEC how to post to a controller? What's wrong with this?

I'm trying to post to my controller in RSPEC, see anything wrong with this? It's failing w/o error:
it "should store create an IncomingMail record" do
lambda {
post 'create', {
"from" => 'XXX',
"to" => 'XXX',
"cc" => 'XXX',
"subject" => 'XXX',
"message_text" => 'XXX',
"message_html" => 'XXX' }
}.should change { IncomingMail.count }.by(1)
end
Updated:
it "should store create an IncomingMail record" do
post :create,
:from => 'xx',
:to => 'xx',
:cc => 'xx',
:subject => 'xx',
:message_text => 'xx',
:message_html => 'xx'
mail = IncomingMail.last(:order => 'created_at desc')
mail.from.should == 'xx'
end
Controller
class IncomingMailsController < ApplicationController
require 'iconv'
#make sure that rails doesn't raise an exception because we have no way of knowing the token
skip_before_filter :verify_authenticity_token
def create
begin
#incoming_mail = IncomingMail.create(
:from => params[:from],
:to => params[:to],
:cc => params[:cc],
:subject => params[:subject],
:message_text => message_text_utf8,
:message_html => message_html_utf8
)
.....
This is how i do it :
Route Example :
post 'train_ability/:ability' => :train_ability, :as => 'train_ability'
Spec :
it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do
#user.str = 10
#user.str_points = 0
#user.save!
post :train_ability, :ability => 'str'
#user.reload
flash[:error].should be_nil
#user.str_points.should == 1
#user.str.should == 11
end

actionmailer question on rails 3

I'm trying to convert this code
def password_reset_instructions(user)
subject "Registered"
recipients user.email
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
to this code
def password_reset_instructions(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
My problem is i don't know where to put the code below.
:edit_password_reset_url => edit_password_reset_url(user.perishable_token)"
I am using authlogic on rails 3.
In Rails 3, Mailers work just like controllers. You can use the instance variable of the user in the accompanying view.
Not tested, but try this:
def password_reset_instructions(user)
#edit_password_reset_url = edit_password_reset_path(user.perishable_token)
mail(
:subject => "Password Reset Instructions",
:recipients => user.email
)
end

Resources