【问题标题】:php inline style to my contact send formphp 内联样式到我的联系人发送表单
【发布时间】:2017-08-12 04:11:34
【问题描述】:

这是我到目前为止得到的。我的内联代码在哪里?我也有表单被编码的 html 页面,但我将它链接到这个 php 页面。表单效果很好,只是想为客户添加一些样式。

<?php 

// define variables and set to empty values
$name_error = $email_error = $phone_error = $subject_error = "";
$name = $email = $phone = $message = $subject = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
   } else {
    $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $email_error = "Invalid email format"; 
}
}

if (empty($_POST["phone"])) {
  $phone_error = "Phone is required";
 } else {
    $phone = test_input($_POST["phone"]);
     // check if phone number is well-formed
     if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}     [\s-]?\d{4}$/i",$phone)) {
       $phone_error = "Invalid phone number"; 
     }
   }

  if (empty($_POST["subject"])) {
     $subject_error = "Subject is required";
   } else {
    $subject = test_input($_POST["subject"]);
     // check if subject is proper text form
    if (!preg_match("/^[a-zA-Z ]*$/",$subject)) {
       $subject_error = "Invalid URL"; 
     }
  }

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

   if ($name_error == '' and $email_error == '' and $phone_error == '' and      $subject_error == '' ){
       $message_body = '';
       unset($_POST['submit']);
       foreach ($_POST as $key => $value){
           $message_body .=  "$key: $value\n";
       }
       // sends message to an email address specified. 
       $to = 'email@gmail.com';
       $subject = 'Contact Form Submit';
      if (mail($to, $subject, $name, $message)){
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $phone = $message = $subject = '';
       }
   }

}

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

任何帮助将不胜感激!

【问题讨论】:

标签: php html forms styles inline


【解决方案1】:

首先,我认为您的 mail() 函数调用有错误。 使用此功能发送电子邮件的正确方法是:

mail($to, $subject, $message, $headers);

$headers 参数是可选的,但如果您需要发送 stylesh 电子邮件,则需要这个。

这些标题的示例:

$headers = "From: $name<$email>".PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "X-Mailer:PHP/".phpversion()."".PHP_EOL;
$headers .= "Content-Type: text/html; charset=utf-8".PHP_EOL;

使用这些标头,您是在向电子邮件客户端说您正在发送 html 内容,然后,您可以在 $message 中放置一些标签,例如:

$message = "<strong>Name:</strong> $name<br /><strong>Lorem ipsum</strong><br /><hr />Lorem ipsum";

注意:如果你想包含一些图片,只需使用标签,但在 src 中使用 publics url,如下所示:

<img src="http://www.yoursite.com/emails/email-header.jpg" />

【讨论】:

  • 太棒了!谢谢。我应该将
  • 您可以在您的电子邮件内容中放置一个或多个 标签,即 $message 变量。你把 放在你需要的地方。可能作为页眉或页脚,或者可能与一些文本内联。这取决于您想要的设计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
相关资源
最近更新 更多