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
}
Related
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);
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 am trying to create a php program that will allow the user to mass email users, given a certain page. I am loading in a page via php, using the file_get_contents() function.
The site has a list of names (I just need to tag on #gmail.com). There is a lot of unnecessary content and I'd like to only see the anchor tags. If you can also help with the adding on the email domain
you can use the PHP function strip_tags($text, '<a>);
That will remove all HTML and PHP tags, except for tags.
For example:
$text = '<b>This is a header</b><br /><span class="text">This is some text</span>This is a link';
echo strip_tags($text, '<a>');
// Outputs:
This is a headerThis is some textThis is a link
I have a PHP webmail script that I received today message from gmail like this:
$content='
<p>This is just example for MessageContent</
p>Test...<span>Test</span>Test..<span
>Test</span>';
How can I print it with nl2br?
If I use echo nl2br($content);, I get this result:
<br />
<p>This is just example for MessageContent</<br />
p>Test...<span>Test</span>Test..<span<br />
>Test</span>
So how I can fix this problem?
Personally I think accepting html in a message is bad, and you should strip out any html, why?
Because if you accept html tobe rendered. eg: not using htmlentities($content) from an unknown source then a malicious person could add
javascript and XSS attack you, or a dodgy link and CSRF you or even just a 1px image and get your IP.
But if you accept the risk you can just render the message as-is.
<?php
$content='
<p>This is just example for MessageContent</
p>Test...<span>Test</span>Test..<span
>Test</span>';
//Or at least sanitise abit - Remove all tags except p's an a's
$content = strip_tags($content,'<p><a>');
?>
nl2br is for content you expect to have no html in it.
You could either change $content to
$content='
<p>This is just example for MessageContent</p>
Test...<span>Test</span>Test..
<span>Test</span>';
Or you could just use
echo $content;
I have a .php that contents the body of a text email.
In that body I want to create a link that when the user receives the email can be clicked.
How to create that link?
Regards
Javi
If it is as simple as your question sounds then..
<?php
echo 'Click Here';
?>
The HTML a tag will do them job.
This is assuming that youu are using a HTML based email, if you are using plain text then I would suggest just typing in the URL
If it is a plain-text email, you can't have links. The best you can hope for is to write out the url and hope that the mail-reader will convert it into a link for you.
if you use Content-type: text/html
its simple insert a tag in the body
simple code to send mail:
http://www.webhostingtalk.com/showthread.php?t=416467
You can simply use a Tag as in HTML and that use that HTML for the content of your mail.