提问人:Jiew Meng 提问时间:1/24/2011 最后编辑:Andrew BrēzaJiew Meng 更新时间:5/12/2023 访问量:1057327
我可以使用 mailto: 设置电子邮件的主题/内容吗?
Can I set subject/content of email using mailto:?
答:
是的,使用 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>
正如评论中所暗示的,两者都必须正确转义。在每种情况下使用,而不是针对特定情况手动编码。subject
body
encodeURIComponent(subject)
正如 Hoody 在评论中提到的,您可以通过在字符串中添加以下编码序列来添加换行符:
%0D%0A // one line break
评论
mailto:[email protected]?subject=Your+subject
评论
subject
是的,你可以这样:
mailto: [email protected]?subject=something
评论
<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>
您可以使用此代码设置主题、正文、抄送、密件抄送
您可以使用以下任一方式添加添加到 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>
这是诀窍 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>
评论
是的:
使用它来试验 mailto 表单元素和链接编码。
您可以在表单中输入主题、正文(即内容)等,点击按钮并查看可以粘贴到页面中的 mailto html 链接。
您甚至可以指定很少为人所知和使用的元素:抄送、密件抄送、电子邮件。
评论
[Test Zõne]
%5BTest%20Z%C3%B5ne%5D
&#x
我把它分成单独的行,使它更具可读性。
<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>
评论
如果您想将 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 编码的。
评论
请注意,根据 RFC 2368,不能在邮件正文中使用 HTML:
特殊的 hname “body” 表示关联的 hvalue 是消息的正文。“正文”hname 应包含消息的第一个文本/纯正文部分的内容。mailto URL 主要用于生成短文本消息,这些消息实际上是自动处理的内容(例如邮件列表的“订阅”消息),而不是一般的 MIME 正文。
信用:https://stackoverflow.com/a/13415988/1835519
评论
URL 方案在 RFC 2368 中定义。此外,RFC 1738 和 RFC 3986 中定义了将信息编码为 URL 和 URI 的约定。这些规定了如何将 and 标头包含在 URL (URI) 中:mailto:
body
subject
mailto:[email protected]?subject=current-issue&body=send%20current-issue
具体来说,您必须对电子邮件地址、主题和正文进行百分比编码,并将它们放入上述格式。根据 HTML4 标准,百分比编码的文本在 HTML 中使用是合法的,但此 URL 必须经过实体编码才能在属性中使用:href
<a href="mailto:[email protected]?subject=current-issue&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>";
?>
评论
下面是一个可运行的代码片段,可帮助您生成带有可选主题和正文的 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>
我创建了一个开源工具来简化这一点。输入您想要的字符串,您将立即获得:mailto
mailto.now.sh
💌⚡️ 在邮件中模板完整的电子邮件
评论
对于动态使用 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 阻止它。因此,请在本地系统中测试此代码。
您只需要一个普通的 HTML 文档。
<a href="mailto:[email protected]?subject=This is a subject&body=This is body">Send an email</a>
下一个:发送电子邮件意向
评论