Open outlook on button click in jQuery

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


Mail Me

16 comments :

  1. HI Can you please help on How to add attachment in the above code??

    ReplyDelete
    Replies
    1. Hi, 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.

      Below link describe "mailto" Protocol in detail.
      http://msdn.microsoft.com/en-us/library/aa767737%28v=vs.85%29.aspx

      Delete
  2. Hi, Can you please explain how to add formatted HTML to the body. When I try it displays the HTML tags instead.

    Thanks.

    ReplyDelete
    Replies
    1. 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.

      $('.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);
      });

      Delete
    2. 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.

      Thanks.

      Delete
  3. 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!

    ReplyDelete
  4. Hi,

    Thanks 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?

    ReplyDelete
  5. Hi,
    It is not possible with "mailto", You should use mail function of php.

    ReplyDelete
    Replies
    1. Hi,
      Its 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

      Delete
    2. I mean mailto not support formatted html.

      Delete
  6. Hi,

    The 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.

    ReplyDelete
    Replies
    1. Hi Arun,
      You can't add an attachment with Mailto, even you can not use formatted body with MailTo.

      Delete
  7. Hi Nirbhay,

    Ur 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

    ReplyDelete
  8. how can i add
    or \n in email body i am getting error

    ReplyDelete
  9. how can i add br or \n in email body

    ReplyDelete