[英]Line break in body of mailto: link
我想在博客文章下面創建一個“轉發給朋友”鏈接,該帖子打開我的電子郵件客戶端並准備好了消息。 我可以讓它用於單行消息,但我希望能夠在消息體內打破行。 我怎樣才能做到這一點?
<?php
printf(
'<a href="mailto:?subject=%s&body=%s">Forward this to a friend</a>',
the_title_attribute('echo=0'),
sprintf('The link: %s Have a great day! Stefaan', get_the_permalink())
);
?>
如何在上面有空行的新行上開始“祝你有美好的一天!”
鏈接: ...
祝你有美好的一天!
斯特凡
我的博客使用帶有Genesis主題的WordPress,我將我的代碼放入啟用PHP的genesis_footer_entry
鈎子中(參見截圖 - 電子郵件文本是荷蘭語)。
說明
a mailto:
是電子郵件地址的URI方案,與所有其他URI一樣,字符需要轉換為可以通過Internet傳輸的格式。
采用原始代碼的簡化形式來說明以下幾點
printf(<href="mailto:?subject=%s&body=%s">Description</a>, the_title_attribute( 'echo=0' ) , sprintf("content link:%s more content", get_the_permalink())
代碼按以下順序運行
get_the_permalink()
獲取您網頁的網址 sprintf()
將$content
%s
替換為步驟1的結果 print_f()
然后替換 %s
即subject=%s
帶有the_title_attribute( 'echo=0' )
的獲取結果,我假設是頁面或你的網站的標題,然后ii)第二個%s
即body=%s
與步驟2的結果形成一個完整a mailto
鏈接 為了保留換行符,您應該在輸出之前將$content
字符串轉換為URL編碼形式。
在使用rawurlencode()
函數完成的PHP中,它根據RFC 3986將字符串中的換行符\\r\\n
轉換為%0D%0A
。
由於百分號%
被用於轉換規范sprint_f()
你應該把這個字符串傳給rawurlencode()
之后的字符串已經被格式化sprint_f()
因此使用rawurlencode(sprint_f($string, $arg))
解
我已經完整地整理了解決方案,您可以將其復制並粘貼到您的小部件中,以避免因錯誤實施而導致的誤報。
格式1和格式2包含基本相同的代碼,這兩種格式可以說明如何插入換行符,方法是1) pressing enter
OR 2) typing in \\r\\n
,其中需要換行符。
我已經將$content
作為單獨的字符串,以便於編輯和編寫代碼。
格式1
<?php
//Start new line by pressing enter
//IMPORTANT NOTE: do not remove the %s
$content="Hi there, this might be interesting for you
This is the link: %s
Have a great day!
Stefaan
";
//No need to amend the code below
printf(
'<a class="simple-mail-link" href="mailto:?subject=%s&body=%s">
<div class="label">
<div class="dashicons dashicons-admin-plugins"></div>
Forward this to a friend</div></a>',the_title_attribute( 'echo=0' ),
rawurlencode(sprintf( $content,get_the_permalink()))
);
?>
格式2
<?php
//Start new line by adding \r\n
//IMPORTANT NOTE: do not remove the %s
$content="Hi there, this might be interesting for you\r\nThis is the link: %s\r\nHave a great day!\r\nStefaan";
//No need to amend the code below
printf(
'<a class="simple-mail-link" href="mailto:?subject=%s&body=%s">
<div class="label">
<div class="dashicons dashicons-admin-plugins"></div>
Forward this to a friend</div></a>',the_title_attribute( 'echo=0' ),
rawurlencode(sprintf( $content,get_the_permalink()))
);
?>
演示
<a class="simple-mail-link" href="mailto:?subject=Page%20Title&body=Hi%20there%2C%20this%20might%20be%20interesting%20for%20you%0D%0AThis%20is%20the%20link%3A%20http%3A%2F%2Fblahblahblah.com%0D%0AHave%20a%20great%20day%21%0D%0AStefaan"><div class="label"> <div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a></body>
您不能在mailto 正文中使用HTML標記
根據RFC 2368 ,這是不可能的:
特殊的hname“body”表示關聯的hvalue是消息的主體。 “body”hname應該包含消息的第一個text / plain正文部分的內容。 mailto URL主要用於生成實際上是自動處理內容的短文本消息(例如郵件列表的“訂閱”消息),而不是一般的MIME主體。
所以任何解決方案都取決於HTML標簽是不可能的。 我建議的解決方案是使用\\ r \\ n和PHP函數rawurlencode
所以這是代碼
<?php
printf(
'<a class="simple-mail-link" href="mailto:x@y.com?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>',
'My Subject',
rawurlencode(sprintf( "Hi there, this might be interesting for you.\r\nThis is the link: %s.\r\nHave a great day!\r\nStefaan", get_the_permalink()))
);
?>
注意:我嘗試使用帶有http://example.com/test.php的變量替換get_the_permalink()代碼
引用:
根據電子郵件規范,每個電子郵件行都需要以舊的<CR><LF>
對結束 - 因此您需要在sprintf()
字符串中使用\\r\\n
。
"This is the link: %s.\\r\\n Have a great day!\\r\\n Stefaan"
如果\\r\\n
不起作用,你就有源語言限制。 鑒於:
\\r
是ASCII 13
(十六進制0D
) \\n
是ASCII 10
(十六進制0A
) 也許你可以使用" "
?
使用urlencoded字符串表示CR / LF
%0D%0A
你想要休息的地方。
在例子中
<a
class="simple-mail-link"
href="mailto:?subject=hello&body=Hello%0D%0A%0D%0AWorld!"
>
在sprintf()
調用中添加另一個%s
參數,並將換行符char %0A
的URL編碼版本(兩次)傳遞給它:
printf(
'<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>',
'subject',
sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %sHave a great day! Stefaan', "link-here", "%0A%0A" )
);
編輯:此外,您還可以將%0A
添加到字符串本身,但您必須記住通過在它之前添加額外的一個來轉義%
char:
printf(
'<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>',
'subject',
sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %%0A%%0AHave a great day! Stefaan', "link-here" )
);
我決定徹底解決這個問題並解決在這里所做的所有嘗試。 如果您想快速回答 ,請查看當前接受的答案 。
要在mailto:
URL body
參數中使用文本,請對其進行百分比編碼 。 在PHP中,使用rawurlencode()
。 主題也應該被編碼。
$subj = "Whatever subject";
$body = "Multi-\r\nline\r\nbody";
printf(
"mailto:?subject=%s&body=%s",
rawurlencode($subj),
rawurlencode($body)
);
mailto:?subject=Whatever%2Osubject&body=Multi-%0D%0Aline%0D%0Abody
要將它用作HTML鏈接的目標,請將HTML中具有特殊含義的字符替換為實體引用 - 在PHP中使用htmlspecialchars()
,並且Wordpress具有更高級的(和可過濾的) esc_html()
。
$subj = "Whatever subject";
$body = "Multi-\r\nline\r\nbody";
printf(
'<a href="%s">mailto: URL with multi-line body</a>',
htmlspecialchars(sprintf(
"mailto:?subject=%s&body=%s",
rawurlencode($subj),
rawurlencode($body)
), ENT_QUOTES)
);
<a href="mailto:?subject=Whatever%2Osubject&Multi-%0D%0Aline%0D%0Abody">mailto: URL with multi-line body</a>
我相信到現在為止,您知道如何修改PHP代碼以生成這樣的鏈接。
您必須處理多個級別的技術 ,每個技術都有自己的轉義方案:
PHP(例如"\\n"
是帶換行符的字符串,不帶反斜杠和字母n;如果你需要,你需要轉義反斜杠 - "\\\\n"
)
(s)printf(例如,要打印文字%
,你必須寫%%
)
HTML(例如&
開始實體引用;從字面上看,它應該被編碼為實體引用&
)
URL(例如,空格不是URL的有效部分,必須以百分比編碼為%20
)
手動操作太多了,並且在沒有檢查中間步驟的情況下確保它是正確的。 如果你得到任何錯誤的中間步驟,它就會被打破。
安全的方法是手動構建最后一步的產品 ,只有一旦它工作,繼續並嘗試以編程方式生成它。 在這種情況下,這樣的產品是一旦粘貼到瀏覽器的位置欄中的URL就打開了預先填充多行體的郵件編輯器。 您可以從一個簡單的工作示例開始,使其變得越來越復雜 ,直到它滿足您的需求。 當手動構建太繁瑣時,您可能選擇了太復雜的產品 - 選擇最簡單的產品仍然可以按照您想要的方式工作,以后可以增加復雜性。
如果即使手動構建URL也會卡住,那你就不走運了。 要么你必須閱讀更多關於這個主題,尋求幫助,或放棄。 如果不僅僅是您,則問題出在您的瀏覽器和電子郵件客戶端之間。 其中至少有一個有bug。 根據我使用mailto:URL的經驗,這並不罕見。 即使你讓它為你工作,你的博客的訪問者可能沒有那么幸運他們的設置。
我花了很多時間來看看HTML規范中關於我在其他答案和問題本身所呈現的嘗試中看到的問題。 特別:
<br>
)中未編碼的<
和>
HTML實現現在相當強大,甚至HTML規范也變得非常寬松,將&符號( &
)編碼為實體引用( &
在HTML5中稱為字符引用 ),因此通常不會這樣做。 (但HTML 4規范要求編碼。)
在HTML文件中的實際字節級別, HTML5中的引用屬性值可以自由包含幾乎任何字符,除了用於引用它們的引號(必須用字符引用替換)。
*有關詳細信息,請參閱HTML5規范中的預處理輸入流和不明確的&符號 。
這就是為什么即使換行和未編碼的<br>
在引用的屬性值中也可以。 但實際上,它們在href
中並不好(與title
不同),因為......
在語義級別,當解析所有內容時,任何字符串(包括空字符串)都被允許作為屬性值,但特定屬性會添加更多約束 。
這就是HTML咬你的地方 - 指定href
包含可能由空格包圍的有效URL ,但不能包含空格
<
, >
和許多其他角色。 這些字符必須進行百分比編碼 。 對於URL及其語法的實際定義 ,HTML規范遵循其他規范。
處理無效URL是實現定義的 ,這些機制可能在瀏覽器之間不一致。 只要尊重規格。 我的Firefox忽略換行符並保留空格 - URL中也不允許使用空格,但Firefox在對URL執行任何其他操作之前對其進行編碼。
您可以在sprintf中添加<br>
標簽(假設您要為HTML顯示斷行):
sprintf( 'Hi there, this might be interesting for you. This is the link: %s.<br> Have a great day!<br> Stefaan',get_the_permalink() )
編輯:
如果您想要純文本郵件,請使用\\r\\n
和雙引號:
sprintf("Hi there, this might be interesting for you. This is the link: %s.\r\n Have a great day!\r\n Stefaan",get_the_permalink() )
你可能會在那里扔掉一個PHP_EOL
<?php
printf(
'<a class="simple-mail-link" href="mailto:?subject=%s&body=%s"><div class="label"><div class="dashicons dashicons-admin-plugins"></div>Forward this to a friend</div></a>',
the_title_attribute( 'echo=0' ),
sprintf( 'Hi there, this might be interesting for you. This is the link: %s. %sHave a great day! %sStefaan',get_the_permalink(), PHP_EOL, PHP_EOL )
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.