我可以使用 mailto: 设置电子邮件的主题/内容吗?

Can I set subject/content of email using mailto:?

提问人:Jiew Meng 提问时间:1/24/2011 最后编辑:Andrew BrēzaJiew Meng 更新时间:5/12/2023 访问量:1057327

问:

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

html 电子邮件 mailto

评论

2赞 Adriano 4/30/2015
相关 css-tricks.com/snippets/html/mailto-links
15赞 Dawson B 11/9/2017
您可以设置电子邮件的每个部分。这是我构建的一个工具,使它变得非常简单:mailto.now.sh

答:

1707赞 Haim Evgi 1/24/2011 #1

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

mailto 主题示例:

<a href="mailto:n[email protected]?subject=free chocolate">example</a>

mailto 内容:

<a href="mailto:[email protected]?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>

正如评论中所暗示的,两者都必须正确转义。在每种情况下使用,而不是针对特定情况手动编码。subjectbodyencodeURIComponent(subject)

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

%0D%0A // one line break

评论

38赞 Jiew Meng 1/25/2011
是否可以将 HTML 用于正文?
20赞 tripleee 5/16/2013
这完全是特定于客户的。在一般情况下,不可以。
5赞 Oscar Pérez 12/9/2013
作为参考:您不必对主题进行 html 转义
26赞 gordie 1/20/2015
请注意,W3C 要求您按照以下规则对字符串进行编码: 字符应以 NFC 表示,空格应转义为 %20。
7赞 superphonic 12/7/2017
+一个复古的 Angelfire 和 Geocities 链接,给了我一股巨大的怀旧之情
13赞 payne 1/24/2011 #2
mailto:[email protected]?subject=Your+subject

评论

1赞 David 7/8/2016
我不是文本。这是一张桌子。如何添加。subject
0赞 Macumbaomuerte 10/10/2020
@David 你不能在一个主题上放一个表格,你知道电子邮件主题是如何工作的吗?主题是纯文本字符串。您可以在主题上放置的最大基本内容是表情符号
5赞 MeanEYE 1/24/2011 #3

是的,你可以这样:

mailto: [email protected]?subject=something

评论

3赞 nafg 12/20/2016
我不认为空间应该在那里?
0赞 Hassan Raza 2/28/2020
这应该没有空格。
142赞 Simer Twilio Toronto developer 1/24/2011 #4
<a href="mailto:[email protected]?subject=Feedback for 
webdevelopersnotes.com&body=The Tips and Tricks section is great
&[email protected]
&[email protected]">Send me an email</a>

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

29赞 niksmac 5/16/2013 #5

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

<a href="mailto:[email protected]?subject=testing out mailto">First Example</a>

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

 <a href="mailto:[email protected]?subject=testing out mailto&body=Just testing">Second Example</a>

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

<a href="mailto:[email protected]?subject=testing out mailto&body=Just testing&[email protected]&[email protected]">Third
    Example</a>

如何向 mailto 标签添加主题

6赞 Arun Kushwaha 10/27/2014 #6

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

<a href="mailto:[email protected]?subject=Your+tip+on+mailto+links&body=Thanks+for+this+tip">tell a friend</a>

评论

0赞 SuperUberDuper 6/28/2019
是否需要 + 而不是空格?没有在Gmail上也能正常工作
2赞 Peter Johnson 10/14/2020
链接现在已死
7赞 woz 6/19/2015 #7

是的:

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

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

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

评论

0赞 Armfoot 8/28/2015
这个 href 字段编辑器还有一个非常有用的功能是能够将所有字符编码为十六进制(例如:成为,这可能会避免一些垃圾邮件机器人)。[Test Zõne]%5BTest%20Z%C3%B5ne%5D
0赞 Armfoot 8/28/2015
我的意思是,如果您选择对所有字符进行编码,它们都会转换为 HTML 表示法(避免一些垃圾邮件机器人)。&#x
7赞 Bram Vanroy 9/5/2018
这个链接是死的。
10赞 quemeful 1/27/2016 #8

我把它分成单独的行,使它更具可读性。

<a href="

    mailto:[email protected]

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

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

    &[email protected]

    &b[email protected]

">Click here to send email!</a>

评论

2赞 varun aaruru 1/27/2016
这一问题已在5年前得到回答
15赞 quemeful 1/28/2016
今天,当您想邮寄东西时,它仍然被使用
1赞 sed 1/29/2016 #9

如果您想将 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 编码的。

评论

0赞 bytor99999 12/15/2018
您能否发布一个示例,其中正文<a href>只有 href 和它与结束标记之间的文本?谢谢
0赞 sed 12/16/2018
<a href=“<同上>”>这是链接文本</a> @user1567291
0赞 Ε Г И І И О 1/9/2020
这是行不通的。电子邮件客户端只会将所有 html 标签显示为文本,而不是将其作为富文本文档进行处理。
5赞 Jure Sah 8/1/2016 #10

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

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

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

评论

2赞 Quentin 8/11/2016
问题是问如何设置主题和内容。它没有提到格式。虽然在答案中注意这是一件有用的事情,但你遗漏了关于什么是可能的以及如何实现它。没有这个,这是一个评论,而不是一个答案。
1赞 bytor99999 12/15/2018
从法律上讲,我真的很感谢你把这个作为这个问题的答案,因为这正是我需要找出的,也是在问自己。因此,虽然昆汀认为这并不能回答 OP 问题,但它回答了我一直在寻找的这篇文章。非常感谢Jure!
48赞 William Entriken 12/29/2016 #11

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

mailto:[email protected]?subject=current-issue&body=send%20current-issue

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

<a href="mailto:[email protected]?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>";
?>

评论

0赞 William Entriken 1/18/2018
这里有一个更深入的例子,如果你的电子邮件中有一个URL,并且该URL包含需要编码的内容:stackoverflow.com/questions/4744888/...
5赞 congusbongus 10/10/2018 #12

下面是一个可运行的代码片段,可帮助您生成带有可选主题和正文的 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>

83赞 Dawson B 4/21/2019 #13

我创建了一个开源工具来简化这一点。输入您想要的字符串,您将立即获得:mailto

mailto.now.sh

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

enter image description here

评论

2赞 Madhusoodan P 8/28/2022
整洁,这是一个很棒的应用程序!
1赞 Kishan Bharda 1/13/2023
嘿道森,这是一个非常有用的工具。谢谢你。
0赞 Alexandru Bulat 3/9/2023
太棒了,谢谢。为我省去了向客户解释技术细节的麻烦。
0赞 Harsh Patel 3/16/2023 #14

对于动态使用 mailto 链接,请参阅下面的代码

        const formConsultation = document.querySelector('form#form-consultation');
        formConsultation.addEventListener("submit", function(evt) {
            evt.preventDefault();
            var targetobj = evt.target;
            var actionUrl = targetobj.action;
            var message = targetobj.message.value;
            if (actionUrl && actionUrl != "#" && message) {
                var newBodyMsg = subject = "";
                var username = targetobj.username.value;
                var useremail = targetobj.useremail.value;

                if (targetobj.subject.value && targetobj.subject.value.trim().length > 0) {
                    subject = targetobj.subject.value;
                }

                if (username.trim().length > 0) {
                    newBodyMsg += "Hi! my name is " + username + ".";
                }
                if (useremail.trim().length > 0) {
                    if (newBodyMsg.length > 0) {
                        newBodyMsg += " & ";
                    }
                    newBodyMsg += "my email address is " + useremail + ".";
                }

                if (newBodyMsg.length > 0) {
                    newBodyMsg += "\n\n ";
                }
                newBodyMsg += message;
                window.open(actionUrl + "?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(newBodyMsg));
            }
        });
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">

<body>
  <div class="container">
    <form action="mailto:[email protected]" method="get" enctype="text/plain" class="form-consultation" id="form-consultation">
      <div class="mb-3">
        <label class="form-label">Name</label>
        <input type="text" class="form-control" name="username" placeholder="Your Name" required>
      </div>
      <div class="mb-3">
        <label class="form-label">Email</label>
        <input type="email" class="form-control" name="useremail" placeholder="Your Email address" required>
      </div>
      <div class="mb-3">
        <label class="form-label">Subject</label>
        <input type="text" class="form-control" name="subject" placeholder="Subject" required>
      </div>
      <div class="mb-3">
        <label class="form-label">Message</label>
        <textarea cols="30" rows="3" class="form-control" name="message" placeholder="Message" required></textarea>
      </div>
      <button type="submit" class="btn btn-primary">Send A Message</button>
    </form>
  </div>
</body>


或者使用 Jquery

$('#form-consultation').submit(function() {
    event.preventDefault(); // this will prevent the default submit

    var actionUrl = $(this).attr('action');
    var message = $(this).find("textarea[name=message]");
    if(actionUrl && actionUrl != "#" && message) {
        var newBodyMsg = subject = "";

        var username = $(this).find("input[name=username]").val();
        var useremail = $(this).find("input[name=useremail]").val();
        if(username.trim().length > 0) {
            newBodyMsg += "Hi! my name is " + username + ".";
        }
        if(useremail.trim().length > 0) {
            if(newBodyMsg.length > 0) {
                newBodyMsg += " & ";
            }
            newBodyMsg += "my email address is " + useremail + ".";
        }

        if (newBodyMsg.length > 0) {
            newBodyMsg += "\n\n ";
        }
        newBodyMsg += message.val();
        window.open(actionUrl + "?subject="+ encodeURIComponent(subject) +"&body=" + encodeURIComponent(newBodyMsg));
    }
    
    return false;
});

注意:- 在您的系统中运行上述代码。如果您尝试在此处运行代码(stackoverflow 代码段),则它不起作用。stackoverflow 阻止它。因此,请在本地系统中测试此代码。

1赞 Nguyễn Anh Tuấn 5/12/2023 #15

您只需要一个普通的 HTML 文档。

<a href="mailto:[email protected]?subject=This is a subject&body=This is body">Send an email</a>