0
<?php
// configure
$from = 'Demo contact form <[email protected]>';
$sendTo = '105 Questions <[email protected]>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from the website\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
これはちょうど}
で少しコードが混乱してしまい、エラーが発生します。PHPはページロード時に電子メールを送信します
しかし、私は?>
を追加すると動作しますが、ページの読み込み時にも電子メールを送信します。
別の投稿で私はisset
で修正しましたが、「コメントを設定」する前にそれを置くと、まだページが読み込まれています。
phpがページの負荷で実行されます。 'mail()'が実行されるのを止めるロジックがコード内にありません –
PHPの実行を停止するためにコードの先頭に "exit"を追加しますか? – etnuh
提出をチェックして –