995

使用 mailto: 时是否可以设置电子邮件的主题/内容?

4

13 回答 13

1611

是的,使用 mailto 查看所有提示和技巧:http ://www.angelfire.com/dc/html-webmaster/mailto.htm

邮件主题示例:

<a href="mailto:no-one@snai1mai1.com?subject=free chocolate">example</a>

邮件内容:

<a href="mailto:no-one@snai1mai1.com?subject=look at this website&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/">tell a friend</a>

正如评论中提到的,两者都subject必须body正确转义。使用encodeURIComponent(subject)每个,而不是针对特定情况手动编码。

正如Hoody在评论中提到的,您可以通过在字符串中添加以下编码序列来添加换行符:

%0D%0A // one line break
于 2011-01-24T12:42:53.457 回答
133
<a href="mailto:manish@simplygraphix.com?subject=Feedback for 
webdevelopersnotes.com&body=The Tips and Tricks section is great
&cc=anotheremailaddress@anotherdomain.com
&bcc=onemore@anotherdomain.com">Send me an email</a>

您可以使用此代码设置主题、正文、抄送、密件抄送

于 2011-01-24T12:44:05.783 回答
48

mailto:URL 方案在RFC 2368中定义。此外,将信息编码为 URL 和 URI 的约定在RFC 1738RFC 3986中定义。这些规定了如何将bodysubject标头包含到 URL (URI) 中:

mailto:infobot@example.com?subject=current-issue&body=send%20current-issue

具体来说,您必须对电子邮件地址、主题和正文进行百分比编码,并将它们放入上述格式。href百分比编码的文本在 HTML 中使用是合法的,但是根据HTML4 标准,此 URL 必须经过实体编码才能在属性中使用:

<a href="mailto:infobot@example.com?subject=current-issue&amp;body=send%20current-issue">Send email</a>

最普遍的是,这里有一个简单的 PHP 脚本,按照上述方式进行编码。

<?php
$encodedTo = rawurlencode($message->to);
$encodedSubject = rawurlencode($message->subject);
$encodedBody = rawurlencode($message->body);
$uri = "mailto:$encodedTo?subject=$encodedSubject&body=$encodedBody";
$encodedUri = htmlspecialchars($uri);
echo "<a href=\"$encodedUri\">Send email</a>";
?>
于 2016-12-28T16:18:42.217 回答
43

我创建了一个开源工具来简化这个过程。输入你想要的字符串,你会立即得到mailto

mailto.now.sh

⚡️ 在 mailto 中模板完整的电子邮件

在此处输入图像描述

于 2019-04-21T06:53:43.740 回答
28

您可以使用以下任一方式将主题添加到 mailto 命令。将 ?subject out mailto 添加到 mailto 标记。

<a href="mailto:test@example.com?subject=testing out mailto">First Example</a>

我们还可以通过在标签末尾添加 &body 来将文本添加到消息正文中,如下例所示。

 <a href="mailto:test@example.com?subject=testing out mailto&body=Just testing">Second Example</a>

除了正文之外,用户还可以键入 &cc 或 &bcc 来填写 CC 和 BCC 字段。

<a href="mailto:test@example.com?subject=testing out mailto&body=Just testing&cc=test1@example.com&bcc=test1@example.com">Third
    Example</a>

如何将主题添加到 mailto 标签

于 2013-05-16T02:46:13.730 回答
13
mailto:joe@company.com?subject=Your+subject
于 2011-01-24T12:43:36.767 回答
9

我将它分成单独的行以使其更具可读性。

<a href="

    mailto:johndoe@gmail.com

    ?subject=My+great+email+to+you

    &body=This+is+an+awesome+email

    &cc=janedoe@gmail.com

    &bcc=billybob@yahoo.com

">Click here to send email!</a>
于 2016-01-27T14:11:34.703 回答
7

是的:

使用来试验 mailto 表单元素和链接编码。

您可以在表单中输入主题、正文(即内容)等,点击按钮并查看可以粘贴到页面中的 mailto html 链接。

您甚至可以指定鲜为人知和使用的元素:抄送、密送、来自电子邮件。

于 2015-06-19T00:28:34.980 回答
6

是的,你可以这样:

mailto: email@host.com?subject=something
于 2011-01-24T12:43:57.903 回答
6

这是诀窍 http://neworganizing.com/content/blog/tip-prepopulate-mailto-links-with-subject-body-text

<a href="mailto:tips@neworganizing.com?subject=Your+tip+on+mailto+links&body=Thanks+for+this+tip">tell a friend</a>
于 2014-10-27T07:12:50.007 回答
5

请注意,根据RFC 2368 ,不能在邮件正文中使用 HTML :

特殊的 hname “body”表示关联的 hvalue 是消息的正文。“body” hname 应包含消息的第一个文本/纯正文部分的内容。mailto URL 主要用于生成实际上是自动处理内容的短文本消息(例如邮件列表的“订阅”消息),而不是一般的 MIME 正文。

信用:https ://stackoverflow.com/a/13415988/1835519

于 2016-08-01T10:46:31.370 回答
3

这是一个可运行的片段,可帮助您生成 mailto:带有可选主题和正文的链接。

function generate() {
  var email = $('#email').val();
  var subject = $('#subject').val();
  var body = $('#body').val();

  var mailto = 'mailto:' + email;
  var params = {};
  if (subject) {
    params.subject = subject;
  }
  if (body) {
    params.body = body;
  }
  if (params) {
    mailto += '?' + $.param(params);
  }

  var $output = $('#output');
  $output.val(mailto);
  $output.focus();
  $output.select();
  document.execCommand('copy');
}

$(document).ready(function() {
  $('#generate').on('click', generate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="email" placeholder="email address" /><br/>
<input type="text" id="subject" placeholder="Subject" /><br/>
<textarea id="body" placeholder="Body"></textarea><br/>
<button type="button" id="generate">Generate & copy to clipboard</button><br/>
<textarea id="output">Output</textarea>

于 2018-10-10T01:09:23.920 回答
1

如果您想在电子邮件中添加 html 内容,请对邮件正文的 html 代码进行 url 编码并将其包含在您的 mailto 链接代码中,但问题是您无法将此链接中的电子邮件类型从纯文本设置为 html,默认情况下,使用该链接的客户端需要他们的邮件客户端发送 html 电子邮件。如果您想在这里测试一个简单的 mailto 链接的代码,链接中包含一个图像(为可见性添加了角度样式的 url):

<a href="mailto:?body=%3Ca%20href%3D%22{{ scope.url }}%22%3E%3Cimg%20src%3D%22{{ scope.url }}%22%20width%3D%22300%22%20%2F%3E%3C%2Fa%3E">

html 标签是 url 编码的。

于 2016-01-29T11:54:10.397 回答