php - PHP Onsubmit 发送电子邮件
问题描述
我有一个表格,接待员输入访客信息、名字、电子邮件等,我想使用表格中输入的电子邮件地址向访客发送电子邮件。
我有 php 邮件功能,但它目前只发送给我手动指定的特定发件人。如何使此表单上的提交按钮根据电子邮件字段的内容发送电子邮件?
我想我需要做这样的事情
<?php
$to = $row['email'];
$subject = 'Welcome ' . $row['first_name'] . ';
$message = 'You have been booked in';
$headers = 'From: noreply@blah.com' . "\r\n" .
'Reply-To: noreply@blah.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
我不认为 $row[] 是正确的,因为我想从表单中提取,而不是从表单输入的表中提取。
这是表单页面:
//serve POST method, After successful insert, redirect to customers.php page.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//Mass Insert Data. Keep "name" attribute in html form same as column name in mysql table.
$data_to_store = filter_input_array(INPUT_POST);
//Insert timestamp
$db = getDbInstance();
$last_id = $db->insert ('tb_bookings', $data_to_store);
if($last_id)
{
$_SESSION['success'] = "Visitor signed in successfully!";
header('location: bookings.php');
exit();
}
}
//We are using same form for adding and editing. This is a create form so declare $edit = false.
$edit = false;
require_once('includes/admin-header.php');
?>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Manual Sign-in</h2>
</div>
</div>
<form class="form" action="" method="post" id="visitor_form" enctype="multipart/form-data">
<?php include_once('../forms/prebook_form.php'); ?>
</form>
</div>
这是形式:
<fieldset>
<div class="form-group">
<label for="f_name">First Name *</label>
<input type="text" name="first_name" value="<?php echo $edit ? $tb_bookings['first_name'] : ''; ?>" placeholder="First Name" class="form-control" required="required" id = "first_name" >
</div>
<div class="form-group">
<label for="l_name">Last name *</label>
<input type="text" name="last_name" value="<?php echo $edit ? $tb_bookings['last_name'] : ''; ?>" placeholder="Last Name" class="form-control" required="required" id="last_name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" value="<?php echo $edit ? $tb_bookings['email'] : ''; ?>" placeholder="E-Mail Address" class="form-control" id="email">
</div>
<div class="form-group">
<label>Visiting Date</label>
<input name="visiting_date" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>" placeholder="Visiting Date" class="form-control" type="date">
</div>
<div class="form-group">
<label>Visiting</label>
<input name="visiting" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>" placeholder="Who are they visiting?" class="form-control" id="visiting">
</div>
<div class="form-group text-center">
<label></label>
<button type="submit" class="btn btn-warning" >Save <span class="glyphicon glyphicon-send"></span></button>
</div>
</fieldset>
解决方案
也许是这样的......
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$name = $_POST['first_name'];
$subject = 'Welcome ' . $name;
$message = 'You have been booked in';
$headers = 'From: noreply@blah.com' . "\r\n" .
'Reply-To: noreply@blah.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
推荐阅读
- python - 挣扎于字符串到列表的转换
- laravel-8 - Laravel 8 不允许对“PDO”进行序列化
- proxy - 在 Vue3 中访问代理对象
- php - Laravel 8x 响应下载:二进制文件总是 0 字节
- java - 使用回调从远程服务向IPC客户端发送数据时Android AIDL空指针异常
- python - python脚本运行后ModuleNotFoundError
- php - Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException laravel 8 错误
- node.js - 机器人如何从任务模块提交的上下文中发送私人消息?
- ios - 是否应该在应用程序启动到后台时调用 viewcontroller llfecycle 事件?
- json - 从 API 读取 JSON 数据到 Python