使用主题中生成的ID发送电子邮件

时间:2017-02-27 04:27:13

标签: javascript random mailto

所以这里是小提琴的链接。我已经尝试了很多搜索并尝试组合其他网站的代码,但似乎无法弄清楚错误。 https://jsfiddle.net/itsmrchadd/fqjhu03r/2/

基于主题,我只需要弹出具有ID的Outlook。对不起,我有点初学者。 :)

随机发生器已经工作了我能够分别测试它。唯一的问题是发送电子邮件。如果你试图点击链接没有任何反应,它只会加载几秒钟然后停止。



function generateEmailID(length, chars) {
	"use strict";
	
	//Set default values
	var result = '';
	var timestamp = +new Date().toString(36).slice(2);
    
	//Set optional values
	length = length || 7;
	chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	
	//Generate the id based on the parameters with timestamp
  	for (var i = length; i > 0; --i) {
		result += chars[Math.round(Math.random() * (chars.length - 1))];
	}
	return timestamp + result;
}

function sendEmail(email, subject, body) {
	"use strict";
	
	//Set optional values
  	email = email || "random@random.random";
	subject = subject + " [" + generateEmailID() + "]" || "Test [" + generateEmailID() + "]";
  	body = body || "Test";
  
  	//Send email with id generated in the subject
	window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body;
}

<a href="#" onclick="sendEmail()">Send Email</a>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

在您的小提琴更新中,javascript设置和 LOAD TYPE 选择最后两个选项之一:

enter image description here

它对我有用。

答案 1 :(得分:1)

小提琴不起作用,因为不知何故链接是在创建js之前创建的(但是在stackoverflow的“运行代码片段”中你的代码确实有效)。

(在JSFiddle中点击F12,在浏览器中查看控制台日志以查找错误。)

要在JSFiddle中修复它,您可以在创建元素之前引用该函数,如下所示:

HTML:

<script>var sendEmail();</script>
<a href="#" onclick="sendEmail()">Send Email</a>

你的js变化很小:

sendEmail = function(email, subject, body) {
//(...rest of code stays the same)

或者在html页面中创建它时,只需将js放在head

<html>
<head>
    <script>
        //(...js here)
    </script>
</head>
<body>
    <!-- html link here -->
    <a href="#" onclick="sendEmail()">Send Email</a>
</body>
</html>

...因此,代码如下:(我添加了一些其他更改,例如对于似乎不必要的代码,即.slice(2),并在未定义时设置subject = '' ...)

function generateEmailID(length, chars) {
	//"use strict";
	
	//Set default values
	var result = '';
	var timestamp = new Date().toString(36); //.slice(2);
    //console.log(timestamp);
    
	//Set optional values
	length = length || 7;
	chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	
	//Generate the id based on the parameters with timestamp
  for (var i = length; i > 0; --i) {
		result += chars[Math.round(Math.random() * (chars.length - 1))];
	}
    //console.log(timestamp + result);
	return timestamp + result;
}

sendEmail = function(email, subject, body) {
	//"use strict";
    if(!subject) subject = '';
	
	//Set optional values
    email = email || "random@random.random";
	subject = subject + " [" + generateEmailID() + "]" || "Test [" + generateEmailID() + "]";
    body = body || "Test";
    
    //Send email with id generated in the subject
    window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body;
}
<a href="#" onclick="sendEmail()">Send Email</a>

答案 2 :(得分:0)

感谢所有帮助。我已经得到了我想要的..... 它现在也和我的htmls一起正常工作

这里是最后小提琴的链接.... https://jsfiddle.net/itsmrchadd/fqjhu03r/3/

&#13;
&#13;
function generateEmailID(length, chars) {
	"use strict";
	
	//Set default values
	var result = '';
	var timestamp = +new Date().toString(36).slice(2);
    
	//Set optional values
	length = length || 7;
	chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	
	//Generate the id based on the parameters with timestamp
  	for (var i = length; i > 0; --i) {
		result += chars[Math.round(Math.random() * (chars.length - 1))];
	}
  return timestamp + result;
  
  
}

function sendEmail(email, subject, body) {
	"use strict";
	
  //Set default values
  if (!subject) {
		subject = "Test";
	}
  
	//Set optional values
  	email = email || "random@random.random";
		subject = subject + " [" + generateEmailID() + "]" || "Test";
  	body = body || "Test";
  
  	//Send email with id generated in the subject
	window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body;
}
&#13;
<a href="#" onclick="sendEmail()">Send Email</a>
&#13;
&#13;
&#13;