ActionMailer 未在文本邮件程序中显示空格
我有一个 ActionMailer 控制器应该发送此文件:
/user_mailer/welcome_email.text.erb
这是文件的(示例)内容:
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
[编辑] 这是控制器中的代码:
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
这是 UserMailer < 中的代码ActionMailer::Base
def welcome_email(user)
@user = user
@url = "http://znood.com/"
mail(:to => user.email, :subject => "Welcome to Znood!")
end
这是我收到的电子邮件:
Welcometoznood.com,AbdoAchkarYouhavesuccessfullysigneduptoznood.com,Yourusernameis:blabla.Tologintothesite,justfollowthislink:http://znood.com/.Thanksforjoiningandhaveagreatday!TheZnoodTeam
知道如何包含空格、回车符和换行符吗?
[编辑] 安装 letter_opener gem 后,我在控制台中看到以下内容:
----==_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
我尝试更改“Content-Transfer-Encoding”标头,但它们似乎没有改变。我还尝试为其设置默认值。看起来我们被 7 位编码困住了。
[编辑] 另一个应该帮助我们找到问题的方法是,我尝试将以下参数传递给邮件函数,以查看文件渲染器是否有问题:
mail(:to => user.email, :subject => "Welcome to Znood!") do |format|
#format.text(:content_transfer_encoding => "base64")
format.text { render :text => "Hello there!" }
end
“Hellothere!”也整理出来了。
然后,我尝试了下面的代码,以确定是渲染函数还是邮件函数导致了错误。
mail(:to => user.email, :subject => "Welcome to Znood!") do |format|
format.text { "hello there!" }
end
也整理出来了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,邮件程序应该发送文本电子邮件——这可能发生在客户端。真正的解决方案是同时拥有文本和 HTML 模板(或者,通过欺骗(或只是 Markdown),为两者使用相同的模板,如果它们同样简单的话。
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.
我将设置一个继承自
ActionMailer::Base
的类,然后在其中创建welcome_email
方法。然后在您的控制器中调用FooMailer.welcome_email(@user.email).deliver
。如果您经常这样做,请考虑将其转移到重新请求或延迟工作。至于间距问题,您是否尝试过创建一个
welcome_email.html.erb
并将其发送出去?您可以在welcome_email
方法中指定使用 html(带或不带布局),如下所示:您可以观看 Ryan Bates RailsCast #206 此处 查看此功能的使用情况,他还展示了一种在电子邮件发送之前拦截电子邮件的方法,以便您可以看到他们看起来怎么样。一种更简单的方法是使用他的 letter opener gem 它会在浏览器中打开电子邮件(使用在开发模式)。
I'd setup a class that inherits from
ActionMailer::Base
and then create yourwelcome_email
method there. Then in your controller callFooMailer.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 yourwelcome_email
method to use the html (with or without a layout) like so: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).
这花了我一段时间才解决。问题不在于 ActionMailer。
如果您遇到此问题,请首先确保不是您的代码或 gem 导致此问题。启动一个新的 Rails 应用程序并用它测试 ActionMailer。 (感谢@RubyCyanide 的建议)
就我而言,它是我的
String class
初始值设定项中的一个函数join
。您或您正在使用的 gem 很可能与 Mail 的 join 功能发生冲突。希望这有帮助!
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 myString 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!