0

好的,所以我有一个我为网站联系表编写的基本表单和电子邮件脚本。出于另一个目的,我想修改这个脚本,从联系表单中,允许用户定义电子邮件将被发送到哪里。我已经尝试了几种方法,但没有任何运气。

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;
}
4

3 回答 3

1

你在定义

/ Destination email address
define('EMAIL_DEST', 'abc@123.com');

然后通过以下方式将电子邮件发送到该地址:

// Send mail
mail(EMAIL_DEST, $message_title, $message_body, $headers);

因此,如果您想将电子邮件发送到提供的电子邮件地址,只需使用

// Send mail
mail($email, $message_title, $message_body, $headers);

因为已经定义了电子邮件地址:

$email = isset($_POST['cf_email']) ? $_POST['cf_email'] : '';
于 2013-08-06T04:49:28.937 回答
0

我不知道这里的 EMAIL_DEST 是什么。你一定在某个地方定义了它。

如果您查看邮件功能

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

第一个字符串在to这里,

所以,我很确定您需要在邮件功能中添加 $email 而不是 EMAIL_DEST,才能将其发送给该特定用户

 mail($email , $message_title, $message_body, $headers);
于 2013-08-06T04:51:26.803 回答
0

我认为问题出在你的Mail Header

// Headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $email;

From你的Mail Header应该是一个电子邮件地址而不是名字。

或者你可以用这种方式指定名字

$headers .= 'From: ' . $name . '<' .$email. '>'; //From: Birthday Reminder <birthday@example.com>

编辑

但是您还需要像其他人说的那样$to在函数中指定参数mail

mail($email , $subject, $message, $headers);
于 2013-08-06T04:56:18.343 回答