电子邮件中的表单内容布局

时间:2015-12-10 14:18:36

标签: php email

想知道是否有人可以通过PHP向我提出一些建议,以便在电子邮件中显示内容?

目前,表单数据直接显示在另一行的下方,并且由于表单很大,因此很难阅读。

如何在两者之间划一条线,看起来像这样?

NAME

EMAIL

ETC.

这是我到目前为止所拥有的:

<?php
function IsInjected($str)
{
$injections = array(
       '(\t+)',
       '(%08+)',
       '(%09+)'
       );

$inject = join('|', $injections);
$inject = "/$inject/i";

if(preg_match($inject,$str))
{
  return true;
}
else
{
  return false;
}
}

function processText($text) {
$text = strip_tags($text);
$text = trim($text);
$text = htmlspecialchars($text);
return $text;
}

$formname=$_POST['formname'];
$spamcode=$_POST['spam_preventer_code'];

switch ($formname) {
    case 0: $to='someone@email.org.uk';
        $spamcodecheck=42;
        $thankyoupage="thankyou.htm";
        break;
    case 1: $to='someone@email.com';
        $spamcodecheck=42;
                    $thankyoupage="thankyou.htm";
        break;
    case 3: $to='someone@email.org.uk';
        $spamcodecheck=42;
                    $thankyoupage="thankyou.htm";
        break;
    case 4: $to='someone@email.org.uk';
        $spamcodecheck=73;
                    $thankyoupage="thankyou.htm";
        break;
        case 5: $to='someone@email.org.uk';
        $spamcodecheck=48;
                    $thankyoupage="thankyou.htm";
        break;
        case 6: $to='someone@email.org.uk';
        $spamcodecheck=64;
                    $thankyoupage="thankyou.htm";
        break;
        case 7: $to='someone@email.org.uk';
        $spamcodecheck=48;
                    $thankyoupage="thankyou.htm";
        break;
}



if (!empty($to)) { 
if(isset($_POST['submit'])){
    if($spamcode==$spamcodecheck) {
        $message="Form Contents \n\n";
        foreach ($_POST as $key => $value) {
            if(!IsInjected($value)) $message=$message.$key."=".processText($value)."]\n";
        }

        $subject = "Form $formname submission";
        mail($to,$subject,$message);
        //echo "Mail Sent. Thank you, we will contact you shortly.";
        $header="Location: $thankyoupage";
            header($header); 
    } else {
        echo "Bad Spam Code, please go back and try again.";
    }
} else {
    echo "No form submit in post data";
}
} else {
echo "Bad Form Name.";
}

?>

2 个答案:

答案 0 :(得分:1)

在汇总字段时,您只需要添加一个额外的换行符\n即可获得双倍空格。

而不是:

if(!IsInjected($value)) $message=$message.$key."=".processText($value)."]\n";

使用此:

if(!IsInjected($value)) $message=$message.$key."=".processText($value)."]\n\n";

答案 1 :(得分:0)

PHP支持HTML电子邮件。它主要取决于您的内容类型。

请参阅了解消息内容类型:
https://en.wikipedia.org/wiki/HTML_email#Multi-part_formats

PHP库发送自定义HTML电子邮件:
https://github.com/PHPMailer/PHPMailer

$to = 'tracey@so.com';

$subject = 'Your Custom HTML Format Email';

$headers = "From: " . strip_tags($_POST['from-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['reply-email']) . "\r\n";
$headers .= "CC: sender@so.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


$message = '<html><body>';
$message .= '<table>';
$message .= '<tr>';
$message .= '<td>Name</td>';
$message .= '<td>John</td>';    
$message .= '</tr>';
$message .= '<tr>';
$message .= '<td>Email</td>';
$message .= '<td>John@site.com</td>';       
$message .= '</tr>';
$message .= '<tr>';
$message .= '<td>Etc</td>';
$message .= '<td>etc . . </td>';        
$message .= '</tr>';
$message .= '</table>';
$message .= '</body></html>';


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