Where do I format my html email? - php

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 />";

Related

How to Send an HTML and CSS Stylized Email Through PHP From Server

I am trying to create an "HTML Mail" on my server. I already Google this an there are lots of tutorial about designing and styleing of HTML Mails like This One but none of them explain how to hire PHP to send this HTML format!?
I already tried this
<?php
$msg ="";
$msg.= '<div style="background:red;">Please Accept Format!</div>';
mail("test#gmail.com","My subject",$msg);
?>
but in GMail inbox I am getting
> <div style="background:red;">Please Accept Format!</div>
Can you please let me know how I can use PHP to send HTML and CSS formatted Mails?
Thanks
If you are sending html email, you must specify that in the headers
<?php
$msg ="";
$msg.= '<div style="background:red;">Please Accept Format!</div>';
mail("test#gmail.com", "My subject", $msg, "Content-type:text/html;charset=UTF-8");
?>
Also there are various libraries which can make your life easier as well. One of the easier ones to install and use is PEAR Mail, which you can ready about # http://pear.php.net/manual/en/package.mail.mail.send.php
I personally use swiftmailer with many projects. What you use is purely up to you, but using such classes simplifies various other tasks you will do with email in due time.

PHP prepare MAILTO with HTML

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));
?>

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.

Sending mail with css is not applying in zend

I am using zend.Following is my piece of code in my Action
.......
// create view object
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/layouts/scripts/');
// assign values
$html->assign('OrderList', $this->view->OrderList);
$html->assign('title', 'Package Slip');
$html->assign('current_date', date("F d, Y"));
// render view
$bodyText = $html->render('test.phtml');
$mail = new Zend_Mail('utf-8');
$mail->setBodyHtml($bodyText)
->setFrom('noreply#metalxplus.com', 'admin')
->addTo('dineshkumar.m#openwavecomp.in')
->setSubject('Test');
$mail->send();
........
When I echo this $bodyText variable before sending mail, I got complete page with css. But when i send it to mail, css is not applying. What i have to do here? Kindly advice
Many email clients utilise old version of html and therefore do not respect css. Gmail for instance will not respect any css you add to your message.
To view output, I suggest using either litmus or email on acid which displays how your message will look across a range of email clients.
First of all, view the source of the e-mail to see if the e-mail even contains the CSS you expect. If you're using relative paths; this might be the source of the issue. When you visit a page on, say, //example.com/hello, a path such as "/css/style.css" will be requested as "//example.com/css/style.css".
An e-mail client has no such luxury; it doesn't have the domainname. You'll have to provide the full path to the CSS, ie "http://example.com/css/style.css".
For this problem what I did is to apply in-line styles and it works fine.

Categories