1

まず、私はプログラマーではないので、必要なものに合わせようとしています。私は数週間、一意のコードを生成し、データを私とフォームの所有者にメールで送信する html フォームに取り組んできました。

私はこれらすべてを機能させ、自分のウェブサイトに組み込むことができました。

ただし、最後の痕跡は、メールを html にフォーマットしたいということです。これについては、さまざまなコードを検索して読んで試しました。ここからでも成功しなかったものもあります。私は、途切れることのない 1 つの文でコードを取得します。

これが私のphpです。下部近くに、メールで必要な情報があります。$email_message

フォームコード:

<div style="position:absolute;left:0px;top:546px;width:350px;height:84px;">
<style type="text/css">
.formtxt{color:white;}
</style>
<?php
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serial = '';

for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 4; $j++) {
    $serial .= $tokens[rand(0, 35)];
}

if ($i < 2) {
    $serial .= '-';
}
}
?>
<form name="contactform" method="post" action="bridalcontact.php">
<table width="350px" align="center">
<tr>
<td valign="top">
<label for="first_name"> <span class="formtxt">First Name*</span></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"> <span class="formtxt">Last Name*</span></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"> <span class="formtxt">Email*</span></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="security"> <span class="formtxt">What colour is the sky *</span></label>
</td>
<td valign="middle">
<input type="text" name="security" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Code</label>
</td>
<td valign="top">
<input type="disabled" name="code" size="20" value='<?php echo "$serial"?>'readonly>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>

PHP コード:

<?php

if(isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "my email address here";
  $email_subject = "Your AODJ voucher";
  function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted.    ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
  }
  // validation expected data exists
  if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['security'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');
    }
  $first_name = $_POST['first_name']; // required
  $last_name = $_POST['last_name']; // required
  $email_from = $_POST['email']; // required
  $security = $_POST['security']; // required
  $code = $_POST['code']; // not required
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  $security_exp = "/blue/";
  if(!preg_match($security_exp,$security)) {
    $error_message .= 'Wrong solar system - sorry.<br />';
  }
  //$string_exp = "/^[A-Za-z .'-]+$/";
  //if(!preg_match($security_exp,$security)) {
  //$error_message .= 'Wrong solar system - sorry.<br />';
  //}
  $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
  $email_message = "Form details below.\n\n";
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  $email_message .= "First Name: ".clean_string($first_name)."\n";
  $email_message .= "Last Name: ".clean_string($last_name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "code: ".clean_string($code)."\n";
  // create email headers
  $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  @mail($email_from, $email_subject, $email_message, $headers);
?>
<?php
  if( !empty( $_POST ) ) {
    header( "Location: bridaloptionsthanks.html" ) ; exit ;
  }
}
?>
4

3 に答える 3

1

テスト済みで、問題なく動作します。これを試してみてください。

[形]

<?php
$tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serial = '';

for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 4; $j++) {
    $serial .= $tokens[rand(0, 35)];
}

if ($i < 2) {
    $serial .= "-";
}
}
?>

<!DOCTYPE html>

<head>

<style type="text/css">
.formtxt{color:black;}
</style>

</head>

<body>


<h1>My first heading</h1>

<p align="left">My first paragraph</p>

<div style="position:absolute;left:0px;top:140px;width:350px;height:84px;">
<form name="contactform" method="post" action="bridalcontact.php">
<table width="350px" align="center">
<tr>
<td valign="top">
<label for="first_name"><span class="formtxt">First Name*</span></label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name"><span class="formtxt">Last Name*</span></label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="email"><span class="formtxt">Email*</span></label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">

<label for="security"> <span class="formtxt">What colour is the sky *</span></label>

</td>
<td valign="middle">
<input type="text" name="security" maxlength="80" size="20">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Code</label>
</td>
<td valign="top">
<input type="disabled" name="code" size="20" value="<?php echo "$serial"?>"readonly>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>

</body>
</html>

[処理]

<?php

if(isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "YOUR EMAIL HERE";
  $email_subject = "Your AODJ voucher";
  function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted.    ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
  }
  // validation expected data exists
  if(!isset($_POST['first_name']) || !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['security'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');
    }
  $first_name = $_POST['first_name']; // required
  $last_name = $_POST['last_name']; // required
  $email_from = $_POST['email']; // required
  $security = $_POST['security']; // required
  $code = $_POST['code']; // not required
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }


  $security_exp = "/blue/";


  if(!preg_match($security_exp,$security)) {

    $error_message .= 'Wrong solar system - sorry.<br />';
  }

  $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
  $email_message = "Form details below.\n\n";
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  $email_message .= "First Name: ".clean_string($first_name)."\n";
  $email_message .= "Last Name: ".clean_string($last_name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "code: ".clean_string($code)."\n";

  // create email headers

$headers = "Content-type: text/html\r\n";
$headers .= 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  @mail($email_from, $email_subject, $email_message, $headers);
?>
<?php
  if( !empty( $_POST ) ) {
    header( "Location: thank_you.php" ) ; exit ;
  }
}
?>

[ありがとうございました]

<?php

echo "Thank you";

?>
于 2013-03-07T04:11:41.407 に答える
1

電子メールをhtmlとして送信するための完全なコードを提供することは、必要なものによっては長い作業になる可能性があります。

  1. あなたのコードでは、サーバーが検証なしでフォームから電子メールを送信することを許可しているため、セキュリティの問題が発生している可能性があることを言及する必要があります。何らかの方法で、誰かがあなたのサーバーを使用して、スクリプトからの投稿を自動化することでスパムを送信する可能性があります。

  2. 電子メールは、マルチパートコンテンツ(プレーンテキストの1つのバージョンとhtmlの1つのバージョン)をサポートする場合、実装が非常に複雑になる可能性があるmime形式に従ってhtmlで送信できます。幸いなことに、ほとんどのクライアントは今日htmlをサポートしています。

  3. うまく設計された美しいhtmlコンテンツを送信したい場合は、htmlとcssを学ぶ必要があります。メールクライアントに注意する必要があります。開発に少し時間がかかるすべてのcss属性を常にサポートしているわけではありません。

于 2013-03-06T03:34:19.690 に答える
1

HTML 形式のメールを送信するには、適切なヘッダー情報を追加する必要があります。

この行を変更します。

$headers = 'From: '.$email_from."\r\n".
...

これに:(そしてドット/連結を追加$headers .=

$headers = "Content-type: text/html\r\n";
$headers .= 'From: '.$email_from."\r\n".
...

補足: 最初のヘッダー行のみにドット/連結がありません。それに続く他のヘッダーはそれを必要とします。PHP.net のマニュアルを参照してください。

次に、必要な場所に HTML を追加します。

マニュアルに従って:

于 2013-03-06T03:28:36.243 に答える