Sending two different email with one common attachment, get merged together? - ruby-on-rails

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.

Related

RestClient mailgun error "message": "'from' parameter is missing"

I am using RestClient in my rails app to send emails.
The following Curl command works:
curl -s "https://api:key-XYZ#api.mailgun.net/v2/sandbox30000.mailgun.org/messages" \
-F from='Mailgun Sandbox <postmaster#sandbox30000.mailgun.org>' \
-F to='me <my-email-id#gmail.com>'\
-F subject='Hello XYZ' \
-F text='Congratulations, you just sent an email with Mailgun! You are truly awesome!'
But when I try the same using RestClient, I get the following error message:
error response !!{
"message": "'from' parameter is missing"
}
error message !!400 Bad Request
RestClient call:
RestClient::Request.execute(
:url => "https://api:key-XYZ#api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
:method => :post,
:from => 'Mailgun Sandbox <postmaster#sandbox30000.mailgun.org>',
:sender => 'Mailgun Sandbox <postmaster#sandbox30000.mailgun.org>',
:to => "my-email-id#gmail.com",
:subject => "Hello XYZ",
:text => "Text body",
:"h:X-My-Header" => "www/mailgun-email-send",
:verify_ssl => false)
When you use Request.execute with multiple payload arguments and header(s) you will need to tell RestClient what type of data it is.
You need to these three fixes to get the equivalent curl-request with RestClient.
Group all post fields into a payload hash
Add the single header into a header hash
Tell RestClient that the payload should be sent as multipart.
The changed request:
RestClient::Request.execute(
:url => "https://api:key-XYZ#api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
:method => :post,
:payload => {
:from => 'Mailgun Sandbox <postmaster#sandbox30000.mailgun.org>',
:sender => 'Mailgun Sandbox <postmaster#sandbox30000.mailgun.org>',
:to => "my-email-id#gmail.com",
:subject => "Hello XYZ",
:text => "Text body",
:multipart => true
},
:headers => {
:"h:X-My-Header" => "www/mailgun-email-send"
},
:verify_ssl => false
)

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

Ruby Contact Mailer not working properly

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

ActionMailer doesn't use default from, or any "from" for that matter

I'm trying to send out "Welcome Emails" to my users, however, the default :from is not working. It is using the user_name I specify in the config/application.rb file.
This is the code I currently have.
config/application.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:user_name => "myemail#gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => 'email_i_want_to_send_from#gmail.com'
def welcome_email
mail(:to => "myemail#gmail.com", :subject => "welcome")
end
end
Instead of receiving an email from email_i_want_to_send_from#gmail.com, a user would receive an email from myemail#gmail.com. (Yes, I am using actual email addresses and passwords when testing).
Anyone have any ideas as to why this is happening? Thanks.
Google doesn't allow you to "spoof" a from address. I used Send Grid to send out my emails.

Why can't I set the FROM name correctly using Gmail in my Rails app?

I am trying to get the emails I send through Gmail from my Ruby on Rails app to be from Ben.
Currently when the email arrives in a (seperate, non-related) Gmail account, in the Inbox it has ben in the from section.
Here are my settings:
setup_mail.rb
# my domain is my_example.com, and the email address I am sending from is ben#my_example.com
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "my_example.com",
:user_name => "ben#my_example.com",
:password => "my_password",
:authentication => "plain",
:enable_starttls_auto => true
}
if Rails.env.development?
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
else
ActionMailer::Base.default_url_options[:host] = "my_example.com"
end
report_mailer.rb
def send_notification_email(notification_details)
subject = "testing_email"
mail(:to => notification_details[:email], :subject => subject, :from => "Ben")
end
And here are the email settings in Gmail:
In the :from part you can actually specify the email address this way :
:from => 'Ben <ben#my_example.com>'
The :from key there is actually the email you're sending it from. GMail does not allow this to be overriden, as it can lead to abuse.

Resources