PHP prepare MAILTO with HTML - php

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?

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.

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

Use rawurlencode function to insert carriage return :
<?php
$body = '
First line
Second line';
header("location: mailto:emailadresse#domain.com?subject=Hello&body=".rawurlencode($body));
?>

Related

Where do I format my html email?

So I have designed a website that Ill be hosting very soon. Everything works including forms. However, When a user sends me emails through the form, the email formatting is very ugly.I want to design my emails, something like this:
https://cdn.tutsplus.com/webdesign/uploads/2013/06/21.png
So when I send an email to my client, it should somewhat look attractive like above.
My question is, where do I code the design of the HTML email? Do I do that in my index.html, where my website content is there? But that doesn't make sense?
Do I create a new HTML file and do the code there? and how do i do it?
Thanks in advance
From a plain ugly mail moving to the "right formatted" template mails is a big jump.
You need to tell what you are using to send the mail. Are you using any frameworks or is it just PHP?
To send mails in HTML format, use the below as part of the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Are you already sending mails in HTML Format and just need help on formatting the HTML? Just remember that most of the email clients and email providers don't support external css files. Gmail even removes the CSS Styles at the head of the HTML. Your safest place for CSS styles will be inline using "Style" tag.
For the fancy formatting, I think the safe way to go is to use Tables (and nested tables if need be) most of the email clients and email providers remove the Div tags also.
If you are using PHPMailer then you can save the HTML code into a variable and assign it to the Body like given below:
$mail->Body = "<!-- your html code goes here -->;
As was mentioned by Els Den Lep, there is no way to guarantee that your mail will appear in the format you want in all the email providers / client. However, if you use a combination of Tables, inline CSS you can be fairly sure of a robust display (very close to what you want).
An inelegant hack will be to convert your HTML as an image and use utilities like GIMP or Photoshop to convert it into an HTML using the slice option. This can be especially useful if you are having a large image where you need to give links to specific areas of the image.
Supposing that the file that handles the sending of your email is called sendmail.php, you can do the styling in your sendmail.php as inline css like so:
$message = 'Contact form.<br />';
$message .= 'Subject:<br />';
$message .= "<h1 style=\"color:blue;margin-left:30px;\">This is a heading.</h1><br />";

IMAP map_search() for ics files

i wants to fetch email having only .ics files using IMAP in php
$emails = imap_search($inbox, 'ALL');
i am using above code for search i need some condition instead of "All" or imap_search by some extension(.ics) which gives me emails having .ics
you ccan used this function to serach the perticular extation in your email.
<?php $keyword=".ics";
$emails = imap_search($inbox,'BODY "'.$keyword.'"', SE_FREE, "UTF-8");
?>
The search key header content-disposition ".ics" may or may not work. Depends on the server.
IMAP isn't quite well defined in this aspect.
HEADER <field-name> <string>
Messages that have a header with the specified field-name (as
defined in [RFC-2822]) and that contains the specified string
in the text of the header (what comes after the colon). If the
string to search is zero-length, this matches all messages that
have a header line with the specified field-name regardless of
the contents.
So the field name has to be as defined in 2822. But email messages contain three kinds of header (the top-level message header, embedded message/822 headers and finally bodypart headers), and all three use that syntax. I suspect that most servers search either the first kind of header or all three, and that the RFC's author had in mind the first two.
So why don't you just try the search key with the target server, and if it works, it works.
Thanks you friends i got the exact solution of my problem
i am opening connection first by imap_open then searching emails
$imap_search($inbox, 'ALL')
then fetching structure of emails by imap_fetchstructure and from that structure i am getting ics files from attachments and finally form attachments i am getting imap body imap_base64(imap_fetchbody($mbox, $mid, 2));
it will gives me actually .ics calendar content

Passing HTML to PHP parameters?

I need to pass HTML to the following function
$response = good_mail_using_mandrill($to,$subject,$body,$from_name,$from_email,$MandrilluserAPIKey,$SubAccountID );
At the moment $body takes the entire HTML content from a MySQL field and sends the API call to Mandrill.
What I find is that certain HTML goes through but others dont. I am not sure what tags in the HTML cause this failure. Are there any restrictions on what content can be sent in the $body variable?
This is driving me nuts. Mandrill says that they are receiving "null" content.
Is there a way to read the API call at delivery?

HTML in php mail function

I have a contact form on my website so users can send me an email but I have run into a problem.
I want to use an HTML link inside the email and I also want the content the user is sending to me to be formated how they would like it.... let me explain.
If a user sends this:
Hello World!
Isnt it a great Day?
without using headers to enable html, then it says formated like that when it reaches me.
If I use headers (MIME) to enable html, to also include a link in the email (which I would like to do), then it reaches me as:
Hello World!Isnt it a great Day?
How can I include html, and also keep the email formatted properly?
Thanks
And hopefully all this makes sense :S
Use nl2br on your message - http://php.net/manual/en/function.nl2br.php
It will replace all newlines with HTML <br>
$message = nl2br($message, false);
The second parameter *is_xhtml* is optional if you want to send an HTML email instead of XHTML

How to show Embedded image in an HTML email as normal images (PHP)

I am creating an email client and going through many problems. Now the latest problem which I am facing is to show embedded images on the HTML email body.
My email body code looks similar to this:
<img alt="image001" src="cid:image001.gif#01CCB988.809DD560" id="Picture_x0020_1" />
Few related posts are found, but they are not useful. Also looking for other possible similar problems while displaying mail contents.
I am using PHP IMAP with POP3 to fetch mails. Currently using gmail as mail server. Sending mails using SMTP (PhpMailer).
Thanks in advance.
In HTML, images are standalones documents with a specific URI (used by src="" attribute).
In your MIME boby, this src attribute refer to a relative MIME part.
So, you'll need to write all MIME parts contents in a distinct file (i.e. /tmp/[md5_MIME_PART_ID])
Then use a CGI script to rewrite the email body and forward specific mime request :
readmail.php
<?
if($_GET['mime']) {
readfile("/tmp/{$_GET['mime']}");
die;
}
$contents = preg_replace("/sid:/", "readmail.php?mime=$1", $contents);
echo $contents;
Of course, a proper database system will be more efficient here, but that's a start
Did you use:
$mail->AddEmbeddedImage(filename, cid, name);
//By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
If yes, what is going wrong? Why are you not happy.
Please note that by default, most email clients do not show embedded images, this is goes for Gmail and Outlook. You can not circumvent that. Its a security / bandwith saving feature. The user would first have to press 'Show images' before they appear.

Categories