Here is my use case:
I have integrated my application with SendGrid, and they provide the user a way to customize the unsubscribe link by adding an element with an [UNSUBSCRIBE] in the HTML. Now, there are two reasons why I do not want to edit my existing email templates (HTML files).
There are a lot of them.
I only want to do this in special cases
So, my thought is that I can tack the HTML onto the end of the existing email body in PHP when I go to send out the email, something like:
$html = $html . "<div style='position:absolute;bottom:0px;width:100%;'>"
. "<div style='margin:0px auto;'>"
. "If you would like to stop receiving these emails <a href='[UNSUBSCRIBE]'>click here</a>"
. "</div>"
. "</div>";
But unfortunately, this only places my link at the bottom of the viewable email, not the entire email. So when the user scrolls down, the link is stuck in the middle of the email! I need to place a style on this element, either inline or through CSS, that will place my link all the way at the bottom no matter what. Any ideas?
Make sure its before </body> tag.
Why not to do string replace?
$html = str_replace([yourDIVcode] . '</body>', '</body>', $html);
Related
I am trying to send an email with one image sitting over the top of a background image, I can get it to work if I do this
$message .= "<tr><td background='https://path-to-background/image.jpg'><img src=https://path-to-image/" . $file. " ></td></tr>";
But the background image repeats
If I inline style it it doesnt work at all like this
$message .= "<tr><td><img src=https://path-to-dir/" . $file. " style='background-image: url('https://path-to-background/image.jpg'); background-repeat: no-repeat;'></td></tr>";
I know it may have something to do with " and ' but Ive tried everything and cant get the dam thing to work,
Any help as always is much appreciated
Thanks
Try moving the style property to the <td> tag.
Example:
$message .= "<tr><td background="path to background" style=\"background: url('path to background') no-repeat;\">Content</td></tr>";
Bear in mind that how emails are displayed on different devices will vary. I would recommend using a tool such as litmus to help with getting email templates where you need them.
Please post the entire code, as it may not be only the message body
thats causing problems.
Are you using php mail() or another library?
Did you set your header correctly to show HTML?
You might want to try your HTML code in the browser to make sure it works, before debugging the email (background image repeats sounds like a html/css error?).
Most Email programs do not show HTML correctly. Another argument for testing HTML first. You'll most likely need to inline css, there are a bunch of free tools if you google css inliner, that add the styles as inline.
I am working in a system that allows for custom system-generated emails to be sent to users.
The system has its own tags (such as [[EMP_NAME]] which outputs the employee's name).
I want to use one of these tags within the body of a <a href="mailto: " link.
However, the tags ( [[EMP_NAME]] ) have their own html tags embedded in them so when it is run, the result of the mailto link displays:
Best regards
<strong>John Smith</strong>
I have tried using <pre > but this just gets included in the mail as well.
I have also tried declaring $strname = <pre>[[EMP_NAME]]</pre> and then including $strname in the link body but with no success.
As this editor is embedded within the software, I'm not sure it's that advanced and I'm probably stretching it as it is - so simplest solution preferred.
Would much appreciate any help with this.
Thanks
I have a function that enables members on a site to message each other; the message is stored in mysql database.
My question now is this: what is the best way to allow members to include a link in the message so that, when rendered, it is rendered as a click-able link.
I've tried the following:
click here
but when I then tried to render it on the page it came out as:
$message = nl2br($this->escapeHtml(trim($this->theMessage[0]['message'])));
echo $message; // click here
the var_dump Values of $messages is:
string '<a href="testpage.html"> click here</a>'
HTML markup is complicated, because when displaying it to the user and someone has injected unsavory HTML into the markup, then you've got an XSS attack on your hands. Imagine an added onclick interception, etc.. Any data from outside is dangerous.
markup language
This is one of the reasons, why markup languages like BBCode and markdown exist.
You don't want every piece of HTML markup, only clean and safe stuff.
Basically, you want to work with a restricted set of "content".
And one way of allowing data from outside is by using an "intermediate" markup language.
It is intermediate, because it is a custom format, which is later transformed into HTML.
This happens here on Stackoverflow, too:
[link](http://google.com) = link
tell your users: "to insert a link, using a special syntax"
save the content to the database.
the content you store to the database is something like:
The message text. And some markdown [link](http://google.com).
when you fetch the message from database, you process the markdown content:
$messageFromDb = 'The message. [http://google.com](google)';
$parsedown = new Parsedown();
$html = $parsedown->text($messageFromDb);
echo $html; // ready to show
Result: <p>The message. <a href="http://google.com">http://google.com</a></p>
There are libraries out there ready for usage, like
http://parsedown.org/
https://github.com/egil/php-markdown-extra-extended
filter html
Another way is to allow HTML, but only an restricted set. You would have to filter the inserted HTML, to pick only the good content and drop the rest.
PHP Extension Tidy: http://php.net/manual/en/book.tidy.php
Libraries like http://htmlpurifier.org/
DOM based HTML filter
Instead of relying on a filter library, you could also come up with a "little" DOM based HTML filter.
The following example re-creates a clean link from a crappy and bad one.
You should also check the URL attributes to ensure they use known-good schemes like http:, and not troublesome like javascript:.
This allows to whitelist the combination of elements, to control the nesting and the content.
<?php
// content from form
$html = 'Message <img title="The Link" /> Link Text';
$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
// filter, then rebuild a clean link
foreach ($dom->getElementsByTagName('a') as $node)
{
// extract the values
$title = $node->nodeValue;
$href = $node->getAttribute('href');
// maybe add a href filter?
// to remove links to bad websites
// and to remove href="javascript:"
// oh boy ... simple questions, resulting in lots of work ;)
// create a new "clean" link element
$link = $dom->createElement('a', $title);
$link->setAttribute('href', $href);
// replace old node
$node->parentNode->replaceChild($link, $node);
}
$html = $dom->saveXML();
// drop html, body, get only html fragment
// http://stackoverflow.com/q/11216726/1163786
$html = preg_replace('~<(?:!DOCTYPE|/?(?:html|body|p))[^>]*>\s*~i', '', $dom->saveHTML());
var_dump($html);
Before
Message <img src="injectionHell.png" title="The Link" /> Link Text
After
Message Link Text
To store "HTML in database"
When storing: use addslashes().
When returning text from DB: apply stripslashes(), before rending
A simple way to attain your goal is to save the message including the <a> tags.
You can use an HTML sanitizer so that you accept <a> link tags from your users while removing any potentially dangerous tags.
Then you wouldn't escape the saved text when you output it.
Have a look at HTML purifier.
Alternatively, you could use a Markdown parser to convert plain text to HTML.
your code removes the html tags and replace it with a written form ...
escapeHtml()
what you need is a function that remove all your html tags except what you desire in this case (link tag)
<a>
here is the function you can add it to your code :
function stripme($msg){
$msg = strip_tags($msg,'<a>');
return $msg ;
}
and then call it for your message like this:
$message = nl2br($this->stripme($this->theMessage[0]['message']));
I would like to change the colour of some text in my email signup confirmation message.
An example string:
e.g $mystring = 'Thank you for signing up!<br>Please visit mysite.com and log in.';
Say i wanted to change the colour of the text after the <br>. How do I change the color of the text after the <br>?
You need to add the according html tags. If you want to change the color of words inside text, you should use the span-tag. For bigger parts you should surround with a div-tag. For your example, you would write something like:
$mystring = 'Thank you for signing up!<br><span style="color: red;">Please visit mysite.com and log in.</span>';
I do not recommend using the span tag for this as others have recommended.
Of course, for web pages that's the way to go, but you want to use it in an e-mail. Sadly, simple CSS still does not work in every e-mail client. You should resort to the deprecated font tag:
$mystring = 'Thank you for signing up!<br><font color="red">Please visit mysite.com and log in.</font>';
This should work in every e-mail client that supports basic HTML formatting.
See also: http://www.reachcustomersonline.com/how-to-code-html-email-newsletters-all-new-version/#step4
If you are using JQuery !
var strings = $mystring;
strings = strings.replace(/\[Thank\syou\]/g, '[<font color="red">Thank you</font>]');
$('element').html(text);
Seriously.. :) You want to change text color with PHP? You can't do that. Just use HTML/CSS.
If you don't have control over the HTML text, then here is something that might work for you:
You can try separating the string with "explode()".
$your_array = explode("<br>", 'Thank you for signing up!<br>Please visit mysite.com and log in.');
echo $your_array[0];
echo '<font color="#000000">'.$your_array[1].'</font>';
Otherwise, just do it with markup..
You need use css. I think it would be better approach to do it by adding a css class in your html like this:
$mystring = "Thank you for signing up!<br><span class='my-class'>Please visit mysite.com and log in.</span>";
then in your css
.my-class{
color: red;
//set other style
}
I made script that sends html email messages to users. However, in Gmail, I can't seem to get the font color working. It works for the first message, but the next messages in the conversation are all displayed in purple.
It only happens when I get the strings from .txt files using file_get_Contents()). If I fill the variables with strings from within my class or enter the message directly rather than using variables gmail displays the html normally. I haven't found any problems with other webmail or mail clients. For the actual sending of the mail I tried both phpmailer and the regular mail() function.
$message = "<html><body><font face='Georgia, Times' color='red'>";
$message .= "<p>Beste " . $this->name . "</p>";
$message .= "<p>" . $parPersonal . "</p>";
$message .= "<p>" . $parOne . "</p>";
$message .= "<p>" . $parTwo . "</p>";
$message .= "<p>" . $parThree . "</p></font></body></html>";
A screenshot from gmail:
On a side note, this script will usually only send 1 email to 1 person at a time and this problem shouldn't appear, it is currently just mailing to my own gmail account for testing purposes. However, I'm extremely curious how I can fix this for future purposes.
Most likely this is due to the improper nesting of the <font> tag.
The <font> tag is an inline element, which should be nested inside your block elements such as the <p> tags.
This leads to very messy HTML, but such is the lot with the rather archaic html engines used by email clients.
You can get good coverage these days using inline styles in your elements (but separate <style> blocks are still poorly covered). See this great resource from CampaignMonitor for what you can use : http://www.campaignmonitor.com/css/
Don't use the <font> tag. That's how you styled html in 1995. Instead try using inline styles:
<p style="color: red; font-family: Georgia, Times">
As a general FYI. Many email clients strip out style sheet information from html based emails. If you have a specific area that you want colored, you may want to consider using images.
That said, you should also be aware that inline styling / excessive use of strong tags which are intended to highlight a CLICK ME link can increase the overall span score of your email message.