I would like to see all of my headers in ActionMailer::Preview
as described in this PR. I was not able to see them/make them appear in our company project, so I started a new project to reproduce the problem, where I cant see them either.
I started the project by the rails guidelines, like so:
rails new blog
bin/rails generate mailer User
Addedwelcome_email
inUserMailer
:
class UserMailer < ApplicationMailer
def welcome_email
# Next line is just to add a custom header
headers[:field_name] = 'value'
mail(to: 'abcd@email.com', subject: 'Welcome to My Awesome Site')
end
end
Added welcome_email.html.erb
as well as welcome_email.text.erb
:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, b;abl;a</h1>
<p>
You have successfully signed up to example.com,
your username is: dsafasfs.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
And the preview:
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview
def welcome_email
UserMailer.welcome_email
end
end
Whenever I check for header fields using binding.pry
or just when I send UserMailer.welcome_email.deliver
from rails console I seem to have all headers defined:
Date: Fri, 07 Jul 2023 22:55:29 -0300
From: from@example.com
To: @user.email
Message-ID: <64a8c2113337b_6ce6ba4798f5@Karcagis-MacBook-Pro.local.mail>
Subject: Welcome to My Awesome Site
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_64a8c20d7f8f1_6ce6ba479780";
charset=UTF-8
Content-Transfer-Encoding: 7bit
field-name: value
and
irb(#<UserMailer:0x0000000117213e88>):006:0> mail.header_fields
=>
[#<Mail::Field 0x73cf8 @name="From" @unparsed_value="from@example.com" @charset="UTF-8" @field=#<Mail::FromField:0x00000001172f1378 @errors=[], @name="From", @element=#<Mail::AddressList:0x00000001171746a8 @addresses=[#<Mail::Address:237200 Address: |from@example.com| >], @group_names=[]>, @value="from@example.com", @charset="UTF-8"> @field_order_id=10>,
#<Mail::Field 0x73d48 @name="To" @unparsed_value="@user.email" @charset="UTF-8" @field=#<Mail::UnstructuredField:0x00000001172be658 @errors=[["To", "@user.email", #<Mail::Field::IncompleteParseError: Mail::AddressList can not parse |@user.email|: Only able to parse up to "@user.email">]], @name="To", @element=nil, @value="@user.email", @charset=#<Encoding:UTF-8>> @field_order_id=13>,
#<Mail::Field 0x73d70 @name="Subject" @unparsed_value="Welcome to My Awesome Site" @charset="UTF-8" @field=#<Mail::SubjectField:0x00000001172be518 @errors=[], @name="Subject", @element=nil, @value="Welcome to My Awesome Site", @charset="UTF-8"> @field_order_id=19>,
#<Mail::Field 0x73d98 @name="Mime-Version" @unparsed_value="1.0" @charset="UTF-8" @field=#<Mail::MimeVersionField:0x00000001172f3b28 @errors=[], @name="Mime-Version", @element=#<Mail::MimeVersionElement:0x000000011717b9d0 @major="1", @minor="0">, @value="1.0", @charset="UTF-8"> @field_order_id=22>,
#<Mail::Field 0x73dc0 @name="Content-Type" @unparsed_value=["multipart", "alternative", {"charset"=>"UTF-8", "boundary"=>"--==_mimepart_64a8c1768d9a3_65fc332c80"}] @charset="UTF-8" @field=#<Mail::ContentTypeField:0x0000000116618840 @main_type="multipart", @sub_type="alternative", @parameters={"charset"=>"UTF-8", "boundary"=>"--==_mimepart_64a8c1768d9a3_65fc332c80"}, @errors=[], @name="Content-Type", @element=#<Mail::ContentTypeElement:0x0000000117459b20 @main_type="multipart", @sub_type="alternative", @parameters=[{"charset"=>"UTF-8"}, {"boundary"=>"--==_mimepart_64a8c1768d9a3_65fc332c80"}]>, @value=["multipart", "alternative", {"charset"=>"UTF-8", "boundary"=>"--==_mimepart_64a8c1768d9a3_65fc332c80"}], @charset="UTF-8">>,
#<Mail::Field 0x73de8 @name="field-name" @unparsed_value="value" @charset="UTF-8" @field=#<Mail::OptionalField:0x00000001172f6328 @errors=[], @name="field-name", @element=nil, @value="value", @charset="UTF-8"> @field_order_id=100>]
so the option to show headers should appear. This line is responsible of rendering that part in the header.
I use:
rails 7.0.6
- the latestruby "3.2.2"
Has anyone experienced this?