2

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?

1
  • you need to set header type as html Commented May 30, 2015 at 11:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I use that and its ok. But somethimes for my user is easier to use personal gmail account... so thanks I will continou using plain text for gmail emails
@MonkeyBusiness you could always send the email using php to a specific email address. For example.. $to="[email protected]";mail($to, $subject, $message); Also, please mark the answer as answered if it has answered your question so other users can use the answer. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.