Dmitri
Dmitri

Reputation: 2457

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?

Upvotes: 0

Views: 197

Answers (1)

sjain
sjain

Reputation: 23344

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.

Upvotes: 1

Related Questions