Send non executable script in body by mailto: - javascript

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();

Related

open the mail on outlook using mailto encode problem

Hi In my webpage I generate the information and using mailto for opening the mail on outlook for the user modify the email. It worked fine. However when the body or subject has apostrophe that cause problem, so I used Server.UrlEncode to encode the string. Now, the space show '+' and the new line show '\n'. If I don't use Server.UrlEncode, the function is not called.
There is my code to call the javascript function in vb.net
Dim strSubject As String = Server.UrlEncode(strName)
Dim strBody As String = Server.UrlEncode("it's your order list:" & "\r\n" & strList)
Dim script As String = "MailtoOrder(''," & "'" & strSubject & "', '" & strBody & "')"
If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "mail") Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "mail", script, True)
End If
There is my javascript:
function MailtoOrder( to, subject, body) {
var email='';
if (to != undefined) {
email=to;
}
email = email + '&subject=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(body);
window.location.href = "mailto:" + email;
}
I get away using Server.UrlEncode. I just replace the apostrophe to be escaped character in strSubject and strBody string. it works.

Add params to URL if page gets reloaded

Heyho, I want to add GET params like?USER_NAME=Max&USER_ID=01 to the URL if the page gets reloaded. I already got a few snippets for changing the URL / adding params. But I need something to detect the page reload and execute a function. Or a way to change the path of the Url directly if the page gets reloaded.
I tried several things like beforeunload and navigation types.
$(window).bind('beforeunload', function() {
// EDIT: vars like uname etc are defined
var newurlgetstring = "?USER_NAME=" + uname + "&USER_EMAIL=" + uemail + "&LAST_NAME=" + lname + "&PRE_NAME=" + fname + "&UTITEL=" + utitel + "&U_ID_T=" + uidt + "&MV=" + mv + "&V=" + uv ;
var newurl = window.location.protocol + "//" + window.location.host +window.location.pathname + newurlgetstring;
window.history.pushState({path:newurl},'',newurl);
});
But I want able to execute a function with beforeunload. I was just able to display a return question.

generating link using Url.Action with jquery href attr

I'm trying to generate a link using following
$(this).parent().attr('href', '#Url.Action("Create", "Home"' + '?clientId=' + clientId + '&clientName=' + clientName);
somewhere I read that I need to isolate this Url.Action with controller and action into variable so I tried with this
var a = '#Url.Action("Create", "Home"';
$(this).parent().attr('href', a + '?clientId=' + clientId + '&clientName=' + clientName);
But this still doesn't work.
In browser I'm getting
http://localhost:1328/Home/Index2/#Url.Action%28%22Create%22,%20%22Home%22?clientId=181&clientName=undefined
Another option is to store the url with data-* attributes and access them. In the View you can add the below attribute:
data-url='#Url.Action("Create", "Home")'
Now you can access this in the script with:
var base = $(this).data('url');
$(this).parent().attr('href', base + '?clientId='+ clientId +'&clientName=' + clientName);
Your script should be on Razor page in order to make #Url.Action helper work.
When you place it there this should work:
//this line should generate /Home/Create string
var urlNoParam = '#Url.Action("Create", "Home")';
//and here you just add params as you want
$(this).parent().attr('href', urlNoParam + '?clientId=' + clientId + '&clientName=' + clientName);

Open an URL followed by a hash sign using location.href or similar

There is a page with a "share at whatsapp" button. This creates and executes an URL like:
whatsapp://send?text=Some text followed by a link - http://link_to_this_page#something
The problem is that the browser (I've tested only with Chrome for now) automatically deletes from the hash sign to onwards.
I have tried the basic:
var href = 'whatsapp://send?text=Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + uri;
I tried too with location.replace(), location.assign() and window.open() with no luck.
So the question is, how can I do? It's imperative to use the hash because it tells to the target page that it has to do some things in javascript (which could take more time to change).
You should be encoding anything that is in the query string.
location.href = href + encodeURIComponent(uri);
You probably should be doing:
var href = 'whatsapp://send?text=';
var text = 'Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + encodeURIComponent(text + uri);

Calling Mailto inside body of another mailto

Is that possible to give mailto inside the body of another mailto?
I have vb.net code through which I am opening outlook window.
I have the below code,
sMsg = User.Redirect("mailto:" + legRev + "& CC=" + cc + "&Subject= " + OuterSubject + "&body=" + Body)
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "showalert", sMsg, True)
Public Function Redirect(ByVal PageName As String) As String
Dim sb As New StringBuilder()
sb.Append("window.location.href='" + PageName + "'; ")
Return sb.ToString()
End Function
In the body string I have
mailto:" + innerTo + "&CC=" + innerCC + "&Subject= " + innerSubject
Problem is I am getting a mail opened with subject set to 'innerSubject' instead of 'OuterSubject'
I think my OuterSubject is getting replaced by InnerSubject.
The body string needs to be escaped with a function like Uri.EscapeDataString. Your URI should end up having only one ? in it, and none of the & characters from your body should be visible.
Example:
mailto:john.doe#example.com?subject=Test+Message&body=mailto%3Ajane.doe%40example.com%3Fcc%3Dbob%40bob.com%26subject%3DReply%2Bto%2Byou

Categories

Resources