douhang1913 2013-08-06 04:46
浏览 18
已采纳

php邮件脚本,允许用户定义电子邮件将发送到的目标地址

OK, so I've got a basic form and email script that I wrote for for a website contact form. For another purpose, I would like to modify this script to, from the contact form, allow the user to define where the email will be sent to. I've tried several methods out there and haven't had any luck.

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>

And the current semi-edited 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' . "
";
    $headers .= 'Content-type: text/html; charset=utf-8' . "
";
    $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;
}
  • 写回答

3条回答 默认 最新

  • doupai8533 2013-08-06 04:49
    关注

    You are defining

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

    and then send the email to this adress via :

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

    So if you want to send the email to the provided email adress just use

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

    Because the email adress is already defined:

    $email = isset($_POST['cf_email']) ? $_POST['cf_email'] : '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟