How to create an HTML Email using JavaScript [duplicate] - javascript

This question already has answers here:
mailto link with HTML body
(10 answers)
Closed 1 year ago.
I would like to create an HTML email using JavaScript so that the draft is ready, and the user can send the email.
Here is what I have tried so far:
location.href = "mailto:"+"isflibrary.net.com.de.xxx"+'?cc='+"emailCC"+'&subject='+("Stuff bought by: ", email)+'&body='+"Visit mywebsite.com!";
Wrong output:
Output that I want:

Is this what you were trying to do?
$(document).ready(function() {
$('#btn').click(function() {
mailto = 'isflibrary.net.com.de.xxx';
emailCC = 'test#emailCC.com';
subject = 'Stuff bought by: ' + mailto;
htmlBody = 'Visit mywebsite.com!';
location.href = "mailto:" + mailto + "?cc=" + emailCC + "&subject=" + subject + "&body=" + htmlBody;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id=btn>e-mail</button>

Related

Send non executable script in body by mailto:

I want to prepare an email to send with mailto:
This email contains a few words and a js script. This script does not need to be executed. It's just for the receiver to copy and paste.
The script :
<script id="myID">var script = document.createElement("script");script.src="script-to-inject.js?id=myID&type=0&name=Name&size=120";document.head.appendChild(script); </script>
And my mailto:
window.location.href = "mailto:"+email+"?subject="+subject+"&body=FewWords"+ script;
When my mail isopen i have something like that :
<script id="myID">var script = document.createElement("script");script.src="script-to-inject.js?id=myID
The end of the script does not appear (after the first &)
How can i fix this ?
Thanks !
You forgot to encode the URL parameters, so the & starts the next parameter.
You can use the encodeURIComponent function:
window.location.href = "mailto:" + encodeURIComponent(email) +
"?subject=" + encodeURIComponent(subject) +
"&body=" + encodeURIComponent("FewWords" + script);
Another, cleaner, way would be to use URLSearchParams:
const url = new URL(`mailto:${encodeURIComponent(email)}`)
url.searchParams.set('subject', subject)
url.searchParams.set('body', 'FewWords' + script)
window.location.href = url
You need to be escaping email, subject, and script properly when setting the href attribute. What if these variables contain the & or the = characters? You can see how this would get misinterpreted.
Try this:
window.location.href = "mailto:"
+ encodeURIComponent(email)
+ "?subject="
+ encodeURIComponent(subject)
+ "&body=FewWords"
+ encodeURIComponent(script);
(I'm not sure that you can pass HTML in the body parameter, by the way, it might get interpreted as plain text.)
You can also use URLSearchParams:
const params = new URLSearchParams();
params.append('subject', subject);
params.append('body', 'FewWords' + script);
window.location.href = 'mailto:' + encodeURIComponent(email) + '?' + params.toString();

force_reply works in the address bar but does not work from code in Google Apps Script

The telegram bot must send a message to the user with force_reply active.
Inserting the link in the address bar works; the bot sends the message and the answer function is already active. The same link, generated by the code in Google Apps Script, does not work; the bot does not send anything.
Working link: (I replaced the id of my bot and the ID of the chat with BOT_TOKEN and CHAT_ID)
https://api.telegram.org/BOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=force_replyTest&reply_markup={"force_reply":true}
code to generate the link
var url = "https://api.telegram.org/bot" + token;
function sendText(idChat,text) {
var response = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + idChat + "&text=" + encodeURIComponent(text) + '&reply_markup={"force_reply":true}');
Logger.log(response.getContentText());
}
if(text == "/p"){
sendText(idChat,"force_reply test");
}
Thank to #TheMaster
Work with this code
var response = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + idChat + "&text=" + encodeURIComponent(text) + '&reply_markup=' + encodeURIComponent('{"force_reply":true}'));

Embed image in mail Body using mailto function [duplicate]

This question already has answers here:
mailto link with HTML body
(10 answers)
Closed 6 years ago.
I am using mailto function my requirement is like to embed image inside mail body
$scope.generateMail = function () {
var graph = document.getElementById('thumb_graphs_grp');
html2canvas(graph).then(function(canvas) {
var dataURL = canvas.toDataURL();
var image = new Image();
image.src = canvas.toDataURL("image/png");
var imageHTML = "<img " + "src='" + image.src + "' img/>";
var link = "mailto:mail#example.org?subject=Mail request&body="+ imageHTML;
window.location.href = link;
});
As stated in the RFC2368, it's not possible to include HTML with mailto:
The special hname "body" indicates that the associated hvalue is the
body of the message. The "body" hname should contain the content for
the first text/plain body part of the message. The mailto URL is
primarily intended for generation of short text messages that are
actually the content of automatic processing (such as "subscribe"
messages for mailing lists), not general MIME bodies.

jQuery mailto with anchor element

I tried this with umpteen examples we see on the net. But I guess there is none that is simple and works on all browsers (IE 8 and above as well).
I am trying to simply open up Outlook window with mailto link.
Email
JQuery:
$(function () {
$('#emailLink').on('click', function (event) {
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
Granted, I am a jQuery newbie. The above just doesn't work. It just flickers the browser but nothing opens. I guess this is because of window.location.
Is there a simple solution? I want this to work in IE8 & above and in all browsers.
The body is generated automatically (in JSP).
here's working solution:
Email
and the function:
$(function () {
$('#emailLink').on('click', function (event) {
event.preventDefault();
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
If you do not need the address as a text anywhere on the website I would suggest this:
$('a[data-mail]').on('click', function() {
window.location = 'mailto:' + $(this).data('mail')+'#yourdomain.net' + '?subject=Spotflow';
});
The link woud look like this:
Send me a mail
No chance for bots!
$(function () {
$('[name=emailLink]').click(function () {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
.click can be replaced with .mousedown and so on.. or just
$(function () {
$('[name=emailLink]').each(function() {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
Your selector is looking for an ID
$('#emailLink')
But you have only specified the name.
Add id="emaillink" to the anchor tag.
You don't need any javascript/jQuery at all for this, just the following HTML should do:
Email

Send media by mail javascript

I'm trying to send a recorded message by mail, but no mail client or message is opening when send button is clicked even if the function is running,
function sendMail(media) {
$('#send').click(function() {
var link = "mailto:me#example.com"
+ "?cc=myCCaddress#example.com"
+ "&subject=" + escape("This is my subject")
+ "&body=" + media
;
window.location.href = link;
});
}
whats the problem in my script?
In your sendMail function you create a handler for your button. So you need to call sendMail(); once after declaration to create a handler. I think there is no point in creating separate function if you're going to use it just once. You should do this:
$(function() {
$('#send').click(function() {
var link = "mailto:me#example.com"
+ "?cc=myCCaddress#example.com"
+ "&subject=" + escape("This is my subject")
+ "&body=" + media;
window.location.href = link;
});
});

Categories

Resources