如何使用CSS为联系人表单中的外发电子邮件设置样式

时间:2014-07-30 14:32:11

标签: php html css email contact

我正在尝试传出电子邮件的样式 对于联系表单,但我是新来的PHP我尝试过内联样式但是没有     不行。我明白css必须在下 $ emailbody     “* 即可。我已经复制了所有代码,所以没有任何遗漏,希望这会有所帮助。


//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name']; 
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    $formok = false;
    $errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){

    /*ini_set("SMTP","mail.domain.com"); //mail.yoursite.com should be your local smtp
    ini_set("smtp_port","25"); //25 should be your smtp port
    ini_set("sendmail_from","example@email.com"); //info@yoursite.com should be your site email*/


    ini_set("sendmail_from","example@email.com");
    ini_set("SMTP","mail.btconnect.com"); //mail.yoursite.com should be your local smtp
    ini_set("smtp_port","25"); //25 should be your smtp port

    $headers = "From: enquiry@email.co.uk" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


    $emailbody = "

                <html>
                 <body>
                 <head></head>
                  <h1><p>You have recieved a new message from the enquiries from your website.</p></h1>
                  <h2><p>Name / Company:</h2><strong> {$name} </p></strong> 
                  <h2><p>Email Address:</h2>  <strong>{$email}</strong> </p>
                  <h2><p>Telephone:</h2>  <strong>{$telephone} </strong></p>
                  <h2><p>Enquiry: </h2><strong> {$enquiry} <strong></p>
                  <h2><p>Message: </h2><strong> {$message} <strong></p>
                  <h3><p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p></h3>
                 </body>
                </html>
                  ";


            mail("example@email.com","Estates & Law Website Enquiry",$emailbody,$headers);







}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'enquiry' => $enquiry,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);


//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);
  }
}

3 个答案:

答案 0 :(得分:1)

&#34;但我是php的新手Ive尝试了内联样式,但这并不起作用。&#34;

&#34;我已尝试在div和内联样式中对其进行扭曲,因为样式表没有链接到该页面但它不起作用。我想把它包起来,这样我就可以改变背景了。&#34;

由于您还没有提供任何&#34;没有&#34;工作,我为您提供以下示例。

请注意,Gmail等电子邮件客户端会忽略链接的样式表,或者设置在<style></style>标记内,因此必须使用内联样式。

您可以从此处获取并修改以适应,同时遵循相同的语法。

$emailbody = "

            <html>
             <body LINK=\"#ff8080\" VLINK=\"#ff0000\" ALINK=\"a05050\" STYLE=\"background: #000000; color: #80c0c0\">
             <head></head>
              <h1><p>You have recieved a new message from the enquiries from your website.</p></h1>
              <h2><p>Name / Company:</h2><strong> {$name} </p></strong> 
              <h2><p>Email Address:</h2>  <strong>{$email}</strong> </p>
              <h2><p>Telephone:</h2>  <strong>{$telephone} </strong></p>
              <h2><p>Enquiry: </h2><strong> {$enquiry} <strong></p>
              <h2><p>Message: </h2><strong> {$message} <strong></p>
              <h3><p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p></h3>


<div><div STYLE=\"font-weight: bold; margin-left: 30px\">
<span STYLE=\"font-size: x-large; color: #ffffff\">M</span>y wonderful poetry</div> is available if you are REALLY bored. Why not give it a spin?</div> 


 </body>
</html>
  ";

您需要转义双引号",例如\",或在echo内使用单引号。

I.e。:

<div STYLE=\"font-weight: bold; margin-left: 30px\">

<div STYLE='font-weight: bold; margin-left: 30px'>

答案 1 :(得分:0)

电子邮件客户端仅支持CSS的内联样式属性,例如:

 <div style="color:red;font-weight:bold;">Some text</div>

如果您在<head>中编写css,在HTML标记内写下所有样式

,它将无效

另请参阅此列表以查看哪些css属性与主要邮件客户端兼容: https://www.campaignmonitor.com/css/

答案 2 :(得分:-1)

在您的电子邮件正文中 -

  • <head>在体内。标准是头必须在<body>标记之前开始和结束。
  • <head>标记内,添加<style>代码并将您的css代码放入<style>
  • 或者,在html体内尝试内联css。例如<p height=\"auto\"></p>。当然,记得让"\"逃脱。