OK、ウェブサイトの連絡先フォーム用に書いた基本的なフォームと電子メール スクリプトができました。別の目的で、このスクリプトを変更して、連絡先フォームから、ユーザーが電子メールの送信先を定義できるようにします。私はそこにいくつかの方法を試しましたが、運がありませんでした。
HTML:
<form action='/mail.php' id='contact-form' method='post'>
<p>
<label for='cf_name'>Name to send emails as *</label>
<input id='cf_name' name='cf_name' placeholder='Enter your "name"...' required='required' title='Name' type='text' />
</p>
<p>
<label for='cf_email'>Email address to be sent to *</label>
<input id='cf_email' name='cf_email' placeholder='Email address...' required='required' title='Email address' type='email' />
</p>
<p>
<label for='cf_subject'>Message Title *</label>
<input id='cf_subject' name='cf_subject' placeholder='Email Subject...' required='required' title='Email Subject' type='text' />
</p>
<p>
<label for='cf_message'>Message *</label>
<textarea id='cf_message' name='cf_message' placeholder='Message text...' required='required' rows='10' title='Message text'></textarea>
</p>
<p>
<input type='submit' value='Send message' />
</p>
</form>
そして、現在の半分編集された PHP:
<?php
// Destination email address
define('EMAIL_DEST', 'abc@123.com');
// Contact form error message
define('ERROR_MESSAGE', 'Something went wrong, please try to submit later.');
// Contact form message title
define('CONTACT_EMAIL_TITLE', '');
// Contact form success message
define('CONTACT_SUCCESS_MESSAGE', 'Thank you! We will get back to you as soon as possible.');
// Variables assignment
$name = isset($_POST['cf_name']) ? $_POST['cf_name'] : '';
$email = isset($_POST['cf_email']) ? $_POST['cf_email'] : '';
$subject = isset($_POST['cf_subject']) ? $_POST['cf_subject'] : '';
$message = isset($_POST['cf_message']) ? $_POST['cf_message'] : '';
// Send email if required fields are filled
if(!empty($name) && !empty($email) && !empty($subject) && !empty($message)) {
// Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $name;
// Message title
$message_title = CONTACT_EMAIL_TITLE;
// Message
$message_body = "$message";
// Send mail
mail(EMAIL_DEST, $message_title, $message_body, $headers);
// Show success message
echo '<div class="box-success round-3"><div class="box-content"><p>' . CONTACT_SUCCESS_MESSAGE . '</p></div></div>';
exit;
} else {
// Show error message
echo '<div class="box-error round-3"><div class="box-content"><p>' . ERROR_MESSAGE . '</p></div></div>';
exit;
}