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--------------------------------/
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:
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.
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.
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.
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.
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);
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>";