9
votes

I have an ActionMailer controller that's supposed to send this file:

/user_mailer/welcome_email.text.erb

This is the (sample) content of the file:

Welcome to znood.com, <%= @user.name %>

You have successfully signed up to znood.com,
Your username is: <%= @user.email %>.

To login to the site, just follow this link: <%= @url %>.

Thanks for joining and have a great day!

The Znood Team

[edited] This is the code in the controller:

def sendmail
        @user = User.first
        UserMailer.welcome_email(@user).deliver
        render "user_mailer/welcome_email.text"
        #render the file to see what we're supposed to send
end

and this is the code in UserMailer < ActionMailer::Base

 def welcome_email(user)
    @user = user
    @url  = "http://znood.com/"
    mail(:to => user.email, :subject => "Welcome to Znood!") 
  end

This is the email I'm receiving:

Welcometoznood.com,AbdoAchkarYouhavesuccessfullysigneduptoznood.com,Yourusernameis:blabla.Tologintothesite,justfollowthislink:http://znood.com/.Thanksforjoiningandhaveagreatday!TheZnoodTeam

Any clue how to include the spaces, carriage returns and line feeds?

[edit] After installing the letter_opener gem, I see the following in my console:

----==_mimepart_4ea9882a2735c_1c782d964bc18193

Date: Thu, 27 Oct 2011 19:34:50 +0300

Mime-Version: 1.0

Content-Type: text/plain;

 charset=UTF-8

Content-Transfer-Encoding: 7bit

Content-ID: <[email protected]>



Welcometoznood.com,AbdoAchkarYouhavesuccessfullysigneduptoznood.com,Yourusername
is:blabla.Tologintothesite,justfollowthislink:http://znood.com/.Thanksforjoiningandhaveagreatday!TheZnoodTeam

I attempting changing the "Content-Transfer-Encoding" headers but they don't seem to change. I also tried setting a default value for it. It looks like we're stuck with 7bit encoding.

[Edited] Another that should help us find the problem is that I tried passing the following params to the mail function in order to see whether the file renderer is problematic:

   mail(:to => user.email, :subject => "Welcome to Znood!") do |format|
        #format.text(:content_transfer_encoding => "base64")
        format.text { render :text => "Hello there!" }
    end

"Hellothere!" also came out collated.

I then tried the code below to make sure whether it's the render or mail function that's causing the errors.

mail(:to => user.email, :subject => "Welcome to Znood!") do |format|
        format.text { "hello there!" }
end

Also came out collated.

3
What are the CRLF in the source template?Dave Newton
I don't know exactly but I think if we tackle the spaces problem and solve it, we'll solve the CRLF problem too =)Abdo

3 Answers

1
votes

By default, the mailer should be sending text emails--it's possible this is happening on the client side. The real solution is to have both text and HTML templates (or, through trickery (or just Markdown), use the same template for both, if they're equally simple.

0
votes

I'd setup a class that inherits from ActionMailer::Base and then create your welcome_email method there. Then in your controller call FooMailer.welcome_email(@user.email).deliver. If you are doing this a lot consider moving it to resque or delayed job.

As far as having an issue with the spacing, have you tried creating a welcome_email.html.erb and sending that out? You can specify in your welcome_email method to use the html (with or without a layout) like so:

def welcome_email(user)
  @user = user
  @url = "http://znood.com/"
  mail(:to => user.email, :subject => "Welcome to Znood!") do |format|
    format.html #{ render :layout => 'my_layout' }
  end
end

You can watch Ryan Bates RailsCast #206 here to see this being used and also he shows a method of intercepting emails before they're delivered so you can see how they look. A more simple way of doing it is to use his letter opener gem it opens the email in the browser (use in dev mode).

0
votes

This took me a while to resolve. The problem wasn't with ActionMailer.

If you're having this problem, first make sure it's not your code or gems causing this. Start a new rails application and test ActionMailer with it. (Thanks for @RubyCyanide for this suggestion)

In my case, it was a function join that I had in my String class initializer. It's very probable that you or a gem that you're using is conflicting with Mail's join function.

Hope this helps!