-1

How do I use css to style the email that will be sent from my contact form ? I have tried adding css inline styles to the code but it just breaks it. I would just like to add a message and style the font and add an image/ logo.


<?php

include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{

<html>
<h1>This is a test email<h1>

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);

$error = '';

</html>


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}


}
?>

I would like to add this to the first bit of code so it styles the email that is sent from the contact form.

              <span STYLE=\"font-size:20px; color: #333333\"><p>You have recieved a new message enquiry from <a href=\"http://www.google.co.uk\">www.test.co.uk</a> contact form.</p></span>
              <p>
              <p>
              <div STYLE=\"font-size:16px\">
              <p><strong>Name:</strong> {$name} </p>
              <p><strong>Email Address:</strong> {$email}</strong> </p>
              <p><strong>subject:</strong> {$subject} </p>
              <p><strong>Message:</strong> {$message} </p>

              <h3><p></p></h3>

              <div>
              <dr />
              <span STYLE=\"font-style:italic;font-size:large; color: #333333\">This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</span></div> 

             </body>
            </html>"
jl5660
  • 91
  • 8

2 Answers2

1

You can do something like this:

<?php

include 'config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;

if($post) {
    echo '
    <html>
    <head>
        <link href="style.css" rel="stylesheet">
    </head>
    <h1>This is a test email<h1>
    ';

    $name = stripslashes($_POST['name']);
    $email = trim($_POST['email']);
    $subject = stripslashes($_POST['subject']);
    $message = stripslashes($_POST['message']);

    $error = '';

    if(!$error) {
        $mail = mail(WEBMASTER_EMAIL, $subject, $message,
             "From: ".$name." <".$email.">\r\n"
            ."Reply-To: ".$email."\r\n"
            ."X-Mailer: PHP/" . phpversion());
        if($mail) {
            echo 'OK';
        }
    }
    echo '</html>';
}

?>

You have to set your styles, for example, on that css file.

Once you have opened php code (<?php ?>), if you want to include html code you have two options. You can write that code with the echo function (with "" or '' before and after the html code): <?php PHP_CODE echo 'HTML_CODE'; MORE_PHP_CODE ?> or <?php PHP_CODE echo "HTML_CODE"; MORE_PHP_CODE ?>. You can also close the php section and reopen it later: <?php PHP_CODE ?> HTML_CODE <?php MORE_PHP_CODE ?>.

You have to take care about the html code you want the echo function to write. If you open echo with ', you can't put html code inside with that symbol because you will close the echo sentence. The same happens using echo with " character. You can't write html code with " inside.

brunneis
  • 33
  • 6
0

Make sure you close your <?php tag before outputting HTML content, or use echo statements if you'd rather write the HTML from within the <?php tag. Once you do that, I suspect it'll be easier to apply your CSS without issues.

See this question for more information.

Community
  • 1
  • 1
jstricker
  • 2,132
  • 1
  • 30
  • 44