2

私はこのフォームを完成させ、フォームが送信されたときに正常に機能しています。情報は電子メールIDに電子メールで送信されますが、配信時にすべてのコンテンツがプレーンテキストで表示されます。フォーム情報が電子メールIDに到達したときに、太字のフィールド名。

<?php 
$errors = '';
$myemail = 'abc@email.com';//<-----Put Your email address here.


if(
empty ($_POST['fullname']) ||
empty ($_POST['martialstatus']) ||
empty ($_POST['dateofbirth']) ||
empty ($_POST['email']) ||
empty ($_POST['telephone']) ||
empty ($_POST['cell']) ||
empty ($_POST['graduation']) ||
empty ($_POST['yearatt']) ||
empty ($_POST['department']) ||
empty ($_POST['program']) ||
empty ($_POST['permanentaddress']) ||
empty ($_POST['currentemp']) ||
empty ($_POST['designation']) ||
empty ($_POST['selfemp']) ||
empty ($_POST['officeemail']) ||
empty ($_POST['officetele']) ||
empty ($_POST['portfolio']) ||
empty ($_POST['membership']))

{
    $errors .= "\n Error: all fields are required";
}

$fullname = $_POST['fullname'];
$martialstatus = $_POST['martialstatus'];
$dateofbirth = $_POST['dateofbirth'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$cell = $_POST['cell'];
$graduation = $_POST['graduation'];
$yearatt = $_POST['yearatt'];
$department = $_POST['department'];
$program = $_POST['program'];
$permanentaddress = $_POST['permanentaddress'];
$currentemp = $_POST['currentemp'];
$designation = $_POST['designation'];
$selfemp = $_POST['selfemp'];
$officeemail = $_POST['officeemail'];
$officetele = $_POST['officetele'];
$portfolio = $_POST['portfolio']; 
$membership = $_POST['membership'];



if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Membership Information: $fullname";
    $email_body = "You have received a new message. ".
    " Here are the details:\n  Personal Information \n\n 
    Name:                      $fullname \n 
    Martial Status:            $martialstatus \n 
    Date of Birth:             $dateofbirth \n 
    Email:                     $email \n 
    Telephone:                 $telephone \n 
    Cell:                      $cell \n 
    Year of Graduation:        $graduation \n 
    Years Attended:             $yearatt \n
    Department:                $department \n 
    Program Attended:          $program \n 
    Permanent Address:         $permanentaddress \n\n 
    Career Information \n\n 
    Currently Employeed with:  $currentemp \n 
    Designation:               $designation \n 
    Self Employeed:            $selfemp \n 
    Office Email:              $officeemail \n 
    Office Telephone:          $officetele \n 
    Portfolio:                 $portfolio \n\n 
    MemberShip \n\n 
    Type of MemberShip:        $membership \n\n "; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
4

6 に答える 6

5

太字のテキストは、HTML メールで行うことができます。コンテンツ タイプ ヘッダーを設定します。

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";

<strong>次のタグを使用します。

<strong>Name:</strong>                      $fullname <br />

\nHTML メールでは改行が表示されないことを忘れないでください。使用する必要があります<br />

于 2012-04-26T07:39:28.870 に答える
2

電子メールを HTML 形式で送信し、それぞれの行を次のように変更してください。

Email:                     <b>$email</b> \n
于 2012-04-26T07:37:03.460 に答える
2

メールページのドキュメントで指定されているように、必ずヘッダーを追加してください

$headers .= 'MIME-Version: 1.0' . "\r\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

次に、マークアップを使用してメールを作成します

于 2012-04-26T07:39:42.637 に答える
2

Content-typeヘッダーを追加し、送信するメールに HTML タグを追加する必要があります。そう:

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email\n";
$headers .= "Content-type: text/html";

\nまた、 HTML 改行に変換する必要があるため、次のようになります。

$email_body = nl2br($email_body);
mail($to,$email_subject,$email_body,$headers);
于 2012-04-26T07:41:25.757 に答える
2

ヘッダーは常に `\r\n" で終わる必要があります

これは間違っています

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email";

このようになるはずです

$headers = "From: $myemail\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

メールに HTML タグを追加する必要があります

 Name:                      <strong>$fullname</strong><br />
于 2012-04-26T07:41:27.920 に答える
1

主に繰り返しが非常に多く、セキュリティ上の大きな問題があるため、かなり自由に書き直しました。

これについてもさらにいくつかの改善を行うことができますが、それは読者への演習として残されています。

<?php
$errors = '';
$myemail = 'abc@email.com';//<-----Put Your email address here.

$fields = array(
    'fullname' => 'Name',
    'martialstatus' => 'Martial Status',
    'dateofbirth' => 'Date of Birth',
    'email' => 'Email',
    'telephone' => 'Telephone',
    'cell' => 'Cell',
    'graduation' => 'Year of Graduation',
    'yearatt' => 'Years Attended',
    'department' => 'Department',
    'program' => 'Program Attended',
    'permanentaddress' => 'Permanent Address',
    'currentemp' => 'Currently Employeed with',
    'designation' => 'Designation',
    'selfemp' => 'Self Employeed',
    'officeemail' => 'Office Email',
    'officetele' => 'Office Telephone',
    'portfolio' => 'Portfolio',
    'membership' => 'Type of MemberShip',
);

// Don't allow HTML in form.
foreach ($_POST as $key => $value) {
    $_POST[$key] = strip_tags($value);
}

$missing_fields = array_diff_assoc($fields, $_POST);

if (count($missing_fields) > 0) {
    $errors .= "\n Error: all fields are required";
}

if (empty($errors)) {
    $to = $myemail;
    $email_subject = 'Membership Information: ' . $_POST['fullname'];
    $email_body = 'You have received a new message. Here are the details:<br />  Personal Information <br/ ><br/ >';
    foreach (array('fullname', 'martialstatus', 'dateofbirth', 'email', 'telephone', 'cell', 'graduation', 'yearatt', 'department', 'program', 'permanentaddress') as $key) {
      $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
    }

    $email_body .= "Career Information <br /><br />";

    foreach (array('currentemp', 'designation', 'selfemp', 'officeemail', 'officetele', 'portfolio') as $key) {
      $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
    }

    $email_body .= "MemberShip <br /><br />";

    foreach (array('membership') as $key) {
      $email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
    }

    $headers = "From: $myemail\r\n";
    $headers .= "Reply-To: $myemail\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
于 2012-04-26T08:25:45.487 に答える