PHP prepare MAILTO with HTML

1.1k Views Asked by At

Im looking for a solution, to prepare a complete email for opening with a mail client

PHP is used for generating the link.

Here is the usefull part of my code.

$body = str_repalace('&amp','%26',$body);

$mailtarget = "mailto:".$adress."?cc=".$ccs."&Subject=".rawurlencode($subject)."&Body=".$body.
header("Location = $mailtarget");

This solution works fine, unless i use html in the mail, which is necessary (<br>,<hr>, <table>.

If used HTML sometimes nothing gets in Body, and sometimes just a part of the text.

When I mail the HTML with sendmail.exe, it works perfect. But this is not an option, since its required to add attachments manually to the mail.

Is there any way, to get this properly to work?

3

There are 3 best solutions below

0
On

Try: &Body=".urlencode($body);

2
On

It's not possible to include HTML in body's mailto.

You can try to url_encode your HTML and set it as body in your mailto link, but it has a lot of risks of not working in many email clients.

From this answer, it's even defined in the RFC 2368 :

The special hname "body" indicates that the associated hvalue is the body of the message. The "body" hname should contain the content for the first text/plain body part of the message. The mailto URL is primarily intended for generation of short text messages that are actually the content of automatic processing (such as "subscribe" messages for mailing lists), not general MIME bodies.

0
On

Use rawurlencode function to insert carriage return :

<?php
    $body = '
    First line
    Second line';
    header("location: mailto:[email protected]?subject=Hello&body=".rawurlencode($body));
?>