如何打破电子邮件正文中的长 HTML 字符串
How to break long HTML string in email body
我有一个很长的 HTML 字符串作为我的电子邮件正文,我需要插入换行符 ("\n"
) 以将 HTML 分成几行,因为邮件服务器有排长队的问题。
如何在不破坏 HTML 标签的情况下做到这一点?
我不是在谈论在 HTML 中添加新的换行符 (<br>
),我想在字符串中插入换行符,这样当邮件服务器获取 HTML 逐行不应该达到行长度限制。
例如:
如果我有一个 HTML 字符串:
<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
我想插入新的换行符:
<head>\n<title>\nEnter a title, displayed at the top of the window.\n</title>\n</head>\n<body>\n<h1>\nEnter the main heading, usually the same as the title.\n</h1>\n<p>\nBe <b>bold</b> in stating your key points. Put them in a list: \n</p>\n</body>
许多 HTML 电子邮件使用 quoted-printable 编码:
<head><title>Enter a title, displayed at the top of the window.</title></he=
ad><body><h1>Enter the main heading, usually the same as the title.</h1><p>=
Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
在上面的编码中,尾随 =
标记一行的结尾但不会中断它。可以通过相应地连接行来重新组合原始字符串。
幸运的是,无需重新发明轮子。优秀的 Mail gem can do that for you: (and much more, you want to check out multi-part 封邮件)
require 'mail'
mail = Mail.new do
content_type 'text/html'
content_transfer_encoding 'quoted_printable'
body "<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>"
end
puts mail.to_s
输出:
Date: Wed, 03 Jun 2020 16:44:11 +0200
Message-ID: <5ed7b7...>
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<head><title>Enter a title, displayed at the top of the window.</title></=
head><body><h1>Enter the main heading, usually the same as the title.</h1=
><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></=
body>=
自从您标记问题后 ruby-on-rails, you might also want to check out Action Mailer。
我有一个很长的 HTML 字符串作为我的电子邮件正文,我需要插入换行符 ("\n"
) 以将 HTML 分成几行,因为邮件服务器有排长队的问题。
如何在不破坏 HTML 标签的情况下做到这一点?
我不是在谈论在 HTML 中添加新的换行符 (<br>
),我想在字符串中插入换行符,这样当邮件服务器获取 HTML 逐行不应该达到行长度限制。
例如:
如果我有一个 HTML 字符串:
<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
我想插入新的换行符:
<head>\n<title>\nEnter a title, displayed at the top of the window.\n</title>\n</head>\n<body>\n<h1>\nEnter the main heading, usually the same as the title.\n</h1>\n<p>\nBe <b>bold</b> in stating your key points. Put them in a list: \n</p>\n</body>
许多 HTML 电子邮件使用 quoted-printable 编码:
<head><title>Enter a title, displayed at the top of the window.</title></he=
ad><body><h1>Enter the main heading, usually the same as the title.</h1><p>=
Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
在上面的编码中,尾随 =
标记一行的结尾但不会中断它。可以通过相应地连接行来重新组合原始字符串。
幸运的是,无需重新发明轮子。优秀的 Mail gem can do that for you: (and much more, you want to check out multi-part 封邮件)
require 'mail'
mail = Mail.new do
content_type 'text/html'
content_transfer_encoding 'quoted_printable'
body "<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>"
end
puts mail.to_s
输出:
Date: Wed, 03 Jun 2020 16:44:11 +0200
Message-ID: <5ed7b7...>
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<head><title>Enter a title, displayed at the top of the window.</title></=
head><body><h1>Enter the main heading, usually the same as the title.</h1=
><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></=
body>=
自从您标记问题后 ruby-on-rails, you might also want to check out Action Mailer。