If you want to send an email on button click instead of hyperlink, you have use window.location. You can also put subject and body text in email. It is very easy onle onle line code like this.
window.location = 'mailto:nirbhay.ni@gmail.com?subject=TestSubject&body=TestBody';
In this section I have also given an example to send mail with hyperlink.
Note: you can send formeted html in body.
CODE
<!DOCTYPE html> <html> <head> <title>jQuery With Example</title> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('.btnSendEmail').click(function (event) { var email = 'nirbhay.ni@gmail.com'; var subject = 'Jquery With Example'; var emailBody = 'Hi Nirbhay'; window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody; }); }); </script> </head> <body> <div> <button class="btnSendEmail">Send Email</button><br /><br /> <a href="mailto:nirbhay.ni@gmail.com?subject=YourSubject&body=Test Body">Mail Me</a> </div> </body> </html>
DISPLAY
HI Can you please help on How to add attachment in the above code??
ReplyDeleteHi, There is no standardized way to attach a file with html or JavaScript in mail. You have to use any programming language to attach a file in mail.
DeleteBelow link describe "mailto" Protocol in detail.
http://msdn.microsoft.com/en-us/library/aa767737%28v=vs.85%29.aspx
Hi, Can you please explain how to add formatted HTML to the body. When I try it displays the HTML tags instead.
ReplyDeleteThanks.
With the help of ActiveXObject you can send formatted mail body in mailTo. It work on IE but not all browser, In other world this is not the best way. You should use any programming language.
Delete$('.btnSendEmail').click( function(e) {
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject="Test Subject";
mailItem.To = "nirbhay.ni@gmail.com";
mailItem.HTMLBody = "Hi <b>Nirbhay</b> <br />How are you.";
mailItem.display (0);
});
Thanks Nirbhay. I was hoping to achieve a solution which worked across all browsers. Dynamic flash seems to be the other way, but this seems over the top to me.
DeleteThanks.
Hi Can anyone pls tell me why that intranet security popup is coming after clicking send email button..... I am trying to open outlook on clicking of a thumbnail image in jsp. But That intranet security popup is coming first after that if ALLOW is clicked outlook is getting opened. Please help me about HOW TO OPEN OUTLOOK WITHOUT INTRANET SECURITY POPUP GETTING OPENED FIRST. please help me...Thanks in advance!
ReplyDeleteHi,
ReplyDeleteThanks for the code.
I have a question though:
I want the "var emailBody = my php page content which I processed using HTML" .
Is it possible to include that php page content in body?
Hi,
ReplyDeleteIt is not possible with "mailto", You should use mail function of php.
Hi,
DeleteIts Working. I embedded php.
But problem I am facing is, it is not bale to parse the html tags like ,, etc, and new line character like \n.
Thanks&Regards
I mean mailto not support formatted html.
DeleteHi,
ReplyDeleteThe same flow am using in my application. there is any chance to add a attachment automatically once clicked the button ?
else i want to display the current page content in body the mail using Mailto :
I appreciate your response.
Thanks,
Arun.
Hi Arun,
DeleteYou can't add an attachment with Mailto, even you can not use formatted body with MailTo.
Hi Nirbhay,
ReplyDeleteUr post is really good.
I used window.location = 'mailto:nirbhay.ni@gmail.com?subject=TestSubject&body=TestBody'; to open outlook from my script. Is there any way to add attachments to outlook. I am looking for a universal solution that can work in all browsers. Any help is greatly appreciated.
Thanks,
Ajmal
how can i add
ReplyDeleteor \n in email body i am getting error
how can i add br or \n in email body
ReplyDeleteThanks
ReplyDelete