我用 PHP 脚本创建了一个请愿网站,允许签名者签署请愿书并将其显示在我网站的后台。我想要完成的是在脚本中添加一个邮件功能,以便它将签名者信息以及时间戳、IP 地址和用户代理 ID 通过电子邮件发送到我的电子邮件地址。对此的任何帮助将不胜感激。我是 PHP 新手,并且自学如何编写代码。
<?php
include('database/config.php');
include('database/database.php');
$err = '';
if(isset($_POST['submit'])){
$first = addslashes(trim($_POST['first']));
$last = addslashes(trim($_POST['last']));
$glvar = addslashes(trim($_POST['glvar']));
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
//echo $ip;
if(($first!='')&& ($last!='')&& ($glvar!='')){
$database = new Database(HOST, DATEBASE, USERNAME, PASSWORD);
$allUsers = $database->select('user','glvar_id','*',"glvar_id = '".$glvar."'");
//echo $ip;
$checkglvar = 0;
$checkglvar = count($allUsers);
$userData = array(
'first_name' => $first,
'last_name' => $last,
'glvar_id' => $glvar,
'ip' => $ip,
);
if(!$checkglvar) {
$database->insert('user',$userData);
mail("info@mysite.com", "Subject", "message");
header('location:thank-you.html');
} else $err.='<p style="color:red">Ooops! You have already signed the petition</p>';
} else {
if($first=='') $err.='<p style="color:red">Your First Name is empty</p>';
if($last=='') $err.='<p style="color:red">Your Last Name is empty</p>';
if($glvar=='') $err.='<p style="color:red">Your GLVAR ID is empty</p>';
}
}