Search code examples
javascriptjqueryhtmlemailgmail

Send HTML code to gmail


I am writing this code so that when someone wants to send an email using Gmail then they can just use the link template and an automatic email is prepared, so the function is:

            var sendGmail = function(opts){
    var str = 'http://mail.google.com/mail/?view=cm&fs=1'+
              '&to=' + opts.to +
              '&su=' + opts.subject +
              '&body=' + opts.message.replace(/\n/g,'%0A') +
              '&ui=1';
    location.href = str;
}
            sendGmail({
    to: '[email protected]',
    subject: 'Invice number: '+value,
    message: 'Poštovani, \n'+
            ' \n'+
             'Dostavljamo Vam račun za isporučene usluge \n'+
             'Račun je automatski generisan obradom podataka i važeći je bez pečata i potpisa \n'+
             ' \n'+
             '*račun u papirnom obliku šaljemo Vam na zahtev \n'+
             ' \n'+
             'Ovo je link na kome mozete racun skinuti u PDF formatu: http://agroagro.com/netRacuni/tcpdf/examples/pdf.php?kljuc='+data+' \n' +
' \n'+
' \n'+
' \n'+
'Srdačan pozdrav i hvala na ukazanom poverenju. \n'
});

And this code works fine for plain a text email, but how I can send HTML to gmail to create a HTML based email template?

Is that possible with this way?

I tried:

sendGmail({
        to: '[email protected]',
        subject: 'Invice number: '+value,
        message: '<h1>Poštovani </h1> etc....

But this doesn't work. How I can send HTML code to gmail to render that as HTML and not as plain text?


Solution

  • Basically, the answers no

    Rule Number 1: Security

    Vulnerabilities Explained: For security reasons all html from the URL will be stripped. Google takes security in mind and therefore html entities will be removed.

    The function in php which removes the html entities is..

    htmlspecialchars($string);

    And i'm sure gmail will do something similar to this function.

    If you send a message with the url containing the code below, the website is essentially hacked or looks hacked...

    <script>
    alert('You have been hacked');
    </script>
    

    this code could be run like....

    example.com/?email=<script>alert('hacked');</script>
    

    or replacing "<" and ">" with html entity codes

    And that's the reason it is not possible.

    Your best option is to use a server side language. Such as php to send your mail as html. The mail() function will be a good place to start. Referece: http://php.net/manual/en/function.mail.php Remember to change the header-type to render the email as html.