Style a php value in a PHP mail form

Hello i need help styling a code line in a php mail form file. So i have a translation file (see example below) that i use to translate text in the webpage incl, the mail form. This works fine. But can any one tell me how can i style the text that is shown in the email, based on the code below?

It is this line i want to style so that the text in the email is shown as bold and red: You can se that i’m getting the text translation here: $words[$lang][‘EMAIL_RECIVE_MESSAGE’] the last part is just a fall back function.

/–>This line—>/ $message_body = isset($words[$lang][‘EMAIL_RECIVE_MESSAGE’]) ? $words[$lang][‘EMAIL_RECIVE_MESSAGE’].“\r\n\n” : “A new message from FCAB-Website. Information below.\r\n\n”;

You will find this code on line 52.

In short i want to suraound this code with a style.

Thank you for any help.

/----------------------------------Translation file start----------------------------------/


    <?php

    $words = array(
    
    "en" => array(
    
    'EMAIL_RECIVE_MESSAGE' => "New message from FCAB-Website. Information below."),

/---------------End English Translation - Start Swedish Translation------------------------/

	"se" => array(
	
	'SOME_TEXT' => "Nytt meddelande från FCAB-Hemsida. Information nedan."),
	
	);
	
	?>
	
/----------------------------------Translation file end-----------------------------------/

/----------------------------------Form process file start--------------------------------/

	<?php 
	include_once 'translation-file.php';

/* Some code goes here */


/* Email function start*/


/* I WANT TO STYLE THE LINE DIRECTLY BELOW HERE SO THAT IN THE EMAIL THIS TEXT WILL BE BOLD AND RED*/

/*-->This line--->*/ $message_body = isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]['EMAIL_RECIVE_MESSAGE']."\r\n\n" : "A new message from FCAB-Website. Information below.\r\n\n";


    unset($_POST['submit']);

    foreach ($_POST as $key => $value){
    $message_body .=  "$key: $value\r\n\n";
    }

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers = "From: ".$email_from."\r\n".

        'Reply-To: '.$email_from."\r\n" .

        'X-Mailer: PHP/' . phpversion();

        $to = 'patrik@fcab.se';
        $subject = isset($words[$lang]['CONTACT_EMAIL_SUBJECT']) ? $words[$lang]['CONTACT_EMAIL_SUBJECT'] : "FCAB-Homepage. New message.";
        if (mail($to, $subject, $message_body, $headers)){
        $success = isset($words[$lang]['CONTACT_THANKYOU']) ? $words[$lang]['CONTACT_THANKYOU'] : "Message sent, thank you for contacting us!";
        $name = $email = $company = $message = $website = '';

    }

	?>

/----------------------------------Form process file end--------------------------------/

use CSS like in any other HTML

But this is a php file. i have tested like this but it’s not working:
some php code here…

?>
<div class="EMAIL_RECIVE_MESSAGE" style= "color:red">
<php
$message_body = isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]
?>
<div/>
<php

the rest of the php code.....

The fact that you’re generating the email using PHP isn’t relevant.

That bit of code you posted, though, outputs a div opener, then jumps into PHP and assigns a value to a variable, then jumps out again to close the div. Because you’re building up a message string to send via email, you need to add the divs to the message itself:

$message body = '<div class="EMAIL_RECIVE_MESSAGE" style= "color:red">';
$message_body .= isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang];
$message_body .= '<div/>';

Also note that support for CSS in email clients is quite limited, so have a read through that article that @chorn posted.

Thank you for this. And i will have a look at the link.

I tested now but the email is show wrong, i get the div code in the email to like this:
This how the email looks like:

<div class="email_recive_message" style= "color:red">New message from FCAB-Website. Information below.


<div/>Name: Jörgen Åslin

Email: my@email.se

Company: First Construction AB

Website: http://website.se

Message: TEST WITH STYLING THE CREATING MESSAGE.

Are you sending the email as “text/html”? I forget exactly how to do that, but if it’s sent as plain text then it will just show the html, not render it.

Yes i’m using $headers .= “Content-type:text/html;charset=UTF-8” . “\r\n”;
I also changed the code a little:

    $message_body = '<div class="email_recive_message" style= "color:red">';
    $message_body = isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]['EMAIL_RECIVE_MESSAGE']."\r\n\n\n" : "A new message from FCAB-Website. Information below.\r\n\n\n";
    $message_body = '</div>';

Now i’m not getting the DIV but the text is not red.

If you want, you can see the entire code here: mail code

One of the problems is that by removing the ‘.’ from the second and third lines, you’re not appending any more, you’re replacing. So at the end of those three lines, $message_body just has </div> in it.

I had a look at your code, unfortunately it all displays on one line so it’s really hard to read. Perhaps you could post just the part that deals with building the email variables directly.

I’l post all the code here then, thank you for your time:

<?php 

include_once 'languages.php';
$post_website = $words[$lang]["CONTACTUS_WEBSITE"];
$post_company = $words[$lang]["CONTACTUS_COMPANY"];
$post_email = $words[$lang]["CONTACTUS_EMAIL"];
$post_message = $words[$lang]["CONTACTUS_MESSAGE"];
$post_name = $words[$lang]["CONTACTUS_NAME"];

// define variables and set to empty values
$name_error = $email_error = $company_error = $website_error = $message_error = $main_error = "";
$name = $email = $company = $website = $message = $success = "";

$email_from = isset($words[$lang]['EMAIL_FROM_TEXT']) ? $words[$lang]['EMAIL_FROM_TEXT'] : "FCAB-     Webpage@no_reply_fcab.se";

 // $email_recive_message = isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]   ['EMAIL_RECIVE_MESSAGE']."\r\n\n\n" : "A new message from FCAB-Website. Information below.\r\n\n\n";

// form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST[$post_name])) {
$name_error = isset($words[$lang]['CONTACT_NAME_ERROR_TEXT']) ? $words[$lang] ['CONTACT_NAME_ERROR_TEXT'] : "Name is required";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] :  "Error!";
} else {
$name = test_input($_POST[$post_name]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-ZåäöÅÄÖ ]*$/",$name)) {
$name_error = isset($words[$lang]['CONTACT_NAME_ERROR_TEXT']) ? $words[$lang] ['CONTACT_NAME_ERROR_TEXT'] : "Only letters and white space allowed";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] :  "Error!";	  
 }
}

if (empty($_POST[$post_email])) {
$email_error = isset($words[$lang]['CONTACT_EMAIL_ERROR_TEXT']) ? $words[$lang] ['CONTACT_EMAIL_ERROR_TEXT'] : "Email is required";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] : "Error!";
} else {
$email = test_input($_POST[$post_email]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = isset($words[$lang]['CONTACT_EMAIL_ERROR_TEXT_TWO']) ? $words[$lang]['CONTACT_EMAIL_ERROR_TEXT_TWO'] : "Invalid email format";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] : "Error!";
 }
}

if (empty($_POST[$post_company])) {
$company = "";
} else {
$company = test_input($_POST[$post_company]);
}

if (empty($_POST[$post_website])) {
$website = "";
} else {
$website = test_input($_POST[$post_website]);
}

if (empty($_POST[$post_message])) {
$message_error = isset($words[$lang]['CONTACT_MESSAGE_ERROR_TEXT']) ? $words[$lang]['CONTACT_MESSAGE_ERROR_TEXT'] : "Message is reqired. Min, 10 cahracters";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] :  "Error!";	 
} else {
$message = test_input($_POST[$post_message]);
// check if message field is well-formed
 if(strlen($message) < 10){
 $message_error = isset($words[$lang]['CONTACT_MESSAGE_ERROR_TEXT_TWO']) ? $words[$lang]['CONTACT_MESSAGE_ERROR_TEXT_TWO'] : "Message is invalid";
 $main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] : "Error!";	 
}
}

if ($name_error == '' and $email_error == '' and $company_error == '' and $website_error == '' and $message_error == '' and $main_error == ''){
   
	$message_body = utf8_encode (isset($words[$lang]['EMAIL_RECIVE_MESSAGE'])) ? $words[$lang]['EMAIL_RECIVE_MESSAGE']."\r\n\n\n" : "A new message from FCAB-Website. Information below.\r\n\n\n";
			
	unset($_POST['submit']);
	foreach ($_POST as $key => $value){
	$message_body .=  "$key: $value\r\n\n";
	}
	
	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=uft-8\r\n";
	$headers = "From: ".$email_from."\r\n";
			
	$to = 'my@email.se';
	
	$subject = isset($words[$lang]['CONTACT_EMAIL_SUBJECT']) ? $words[$lang]['CONTACT_EMAIL_SUBJECT'] : "FCAB-Homepage. New message.";
	
	if (mail($to, $subject, $message_body, $headers)){
	$success = isset($words[$lang]['CONTACT_THANKYOU']) ? $words[$lang]['CONTACT_THANKYOU'] : "Message sent, thank you for contacting us!";
    $name = $email = $company = $message = $website = '';
  }
 }
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

what code should that be? there is no sign of color or red in it.

this is the original code i tested with this, but not working:

$message_body = '<div class="EMAIL_RECIVE_MESSAGE" style= "color:red">';
$message_body .= utf8_encode (isset($words[$lang]['EMAIL_RECIVE_MESSAGE'])) ? $words[$lang]['EMAIL_RECIVE_MESSAGE']."\r\n\n\n" : "A new message from FCAB-Website. Information below.\r\n\n\n";
$message_body .= '<div/>';

Have you had a look through that article that @chorn posted? There are a lot of limitations on how to format html emails, and most of the guides that I read suggest using inline styles rather than trying to use CSS. Does a ‘div’ ever work in a html email, even if you close it correctly with </div> instead of <div/>? What email client are you receiving the email with? Different email clients handle html emails in different ways, and support differing levels of styling.

What about trying this:

$message_body = '<p style="color:red">';
$message_body .= utf8_encode (isset($words[$lang]['EMAIL_RECIVE_MESSAGE'])) ? $words[$lang]['EMAIL_RECIVE_MESSAGE']."\r\n\n\n" : "A new message from FCAB-Website. Information below.\r\n\n\n";
$message_body .= '</p>';

ETA: Not sure if your \r and \n will have any effect inside a html email, you might need to use line break tags instead.

OK, i tried this but no luck, the email looks like this:

<p style="color:red">New message from My Company. Information below.

</p>Name: Jörgen Åslin

Email: some@email.se

Company: A Company

Website: http://test_website.se

Message: Test message

I’m using Windows live mail 2012 and Outlook 2007 on a Windows 10 mashine. Both shows the same.

Ahh i got it working now, well the color part any way. Stupid i hade it set to plain text at the moment, but now the problem is that all the values in the email: Name: Email: Company: Website: and Message: are all on the same line.

Ahaaa, i got this working as well by changing this:

$message_body .= '</p>' ."<br>";

unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .=  "$key: $value<br><br>";
}

But now i have another problem. Before when i send in plain text the Email and Website values in the email where clickable links. They are not clickable any more. Any idea as to wy please?

Now that you’ve got it working in HTML mode, it will ignore all blank space, carriage return and so on, just like a web browser would, hence my earlier comment about using <br> and <p> tags.

Are you surrounding your links with the appropriate HTML <a href="xxx">link</a> construction? If it worked while you were in plain text mode, it sounds as if your email client was automating that, and if it’s now gone away, it probably figures you can do it for yourself in HTML mode.

Thank you, but how to i gat a link on a form input object?
I have tested somthigs like:
$url = ‘@(http)?(s)?(://)?(([a-zA-Z])([-\w]+.)+([^\s.]+[^\s]*)+[^,.\s])@’;
$string = preg_replace($url, ‘$0’, $string);

But no luck.

Thank’s again for your help.

Well i’l guess it’s not that important that the website address is not a clickable link, but i am how ever trying to add a logo at the top of the email, but it’s not working:

$message_body .= '<img src="//domain_name.se/folder/images/logo.png" alt="FCAB" />';

$message_body = '<p style="font-family:Oswald; font-size:20px; font-weight:400; letter-spacing: 1.1px; color:#ff0000">';
$message_body .= isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]['EMAIL_RECIVE_MESSAGE'] : "A new message from FCAB-Website. Information below.";
$message_body .= '</p>' ."<br>";

Ahha, got it working now:

$email_greating_message = isset($words[$lang]['EMAIL_RECIVE_MESSAGE']) ? $words[$lang]['EMAIL_RECIVE_MESSAGE'] : "A new message from FCAB-Website. Information below.";

$message_body = '<html><body>';
	$message_body .= '<img src="http://domain.se/folder/images/image.png" alt="My Company" />' ."<br> <br><br><br>";
	$message_body .= '<p style="font-family:Oswald; font-size:20px; font-weight:400; letter-spacing: 1.1px;  color:#ff0000">';
	$message_body .= $email_greating_message;
	$message_body .= '</p>' ."<br>";
	$message_body .= '</body></html>';

Now if i only could get the Website link to work :frowning: