quantumpotato
quantumpotato

Reputation: 9767

How do I send line break in a Ruby email?

I'm using \n and they are showing up as literal \n

class TestMailer < ActionMailer::Base 

  def simple_mail(to, subject, body)
    mail(:to => to, :subject => subject, :body => body)
  end

end

TestMailer.simple_mail('[email protected]', 'subject', 'hi\n there').deliver

I see the literal \n in my inbox.

Upvotes: 1

Views: 1748

Answers (2)

saneshark
saneshark

Reputation: 1243

Single quotes only allow you to escape ' and \ within a string. For example

puts 'That\'s all folks'
puts 'This is a single backslash \\'

Whereas double quotes allow you to escape many more escape sequences. They also allow you to embed variables inside of a string literal using interpolation.

Some of the escape sequences available for double quotes include:

  • \" – double quote
  • \ – single backslash
  • \a – bell/alert
  • \b – backspace
  • \r – carriage return
  • \n – newline
  • \s – space
  • \t – tab

Upvotes: 3

sawa
sawa

Reputation: 168101

Use "hi\n there" instead of 'hi\n there'. Still better, use "hi#$/ there".

Upvotes: 0

Related Questions