我正在设计一个慈善网站,我遇到了我无法解决的问题,因为我没有足够的PHP知识。我希望这里有人能够帮助我实现我想要实现的目标...... :)
在网站上有一个表格,供新成员发送他们的个人数据。表单本身工作正常,但我需要做一些调整,以使它更好一点。
我在网页模板上使用的HTML字段是:
以下是我必须发送数据的PHP代码:
<?php
// require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// configure
$from = 'Demo contact form <info@email.com>';
$sendTo = 'Demo contact form <info@email.com>';
$subject = '<Data form: ';
$fields = array('name' => 'Full name:', 'birthday' => 'Birthday:', 'house_adress' => 'House adress:', 'post_adress' => 'Post adress:', 'email' => 'Email:', 'phone' => 'Phone mumber:', 'nickname' => 'Forums nickname:', 'shirt' => 'T-shirt size:', 'sticker' => 'Sticker color:' ); // array variable name => Text to appear in the email
$okMessage = 'Sample success message.';
$errorMessage = 'Sample error message. Try again later.';
$recaptchaSecret = 'xxx-xxxxxxxxxx_xxxxxxxxxx_xxxxxxxxxxxxx';
// let's do the sending
try
{
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
// do not forget to enter your secret key in the config above
// from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
// everything went well, we can compose the message, as usually
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
以下是我想要实现的目标:
在第
行 $from = 'Demo contact form <info@email.com>';
我希望显示发件人电子邮件,该电子邮件应从电子邮件表单字段中删除。
在第
行 $subject = 'Data Form: ';
我希望显示发件人的全名,该名称应从全名表单字段中获取。
在第
行 $emailText = "You have new message from contact form\n=============================\n";
我想显示所有数据,但我想添加一些基本的HTML样式,因此它们不会以纯文本形式发送。
如果可能,我还希望在邮件末尾包含发件人的IP。
有人在这里帮助我完成任务吗?
谢谢!
答案 0 :(得分:-1)
<?php
// require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// configure
$from = '';
$sendTo = 'Demo contact form <info@email.com>';
$subject = '';
/*
array variable name => Text to appear in the email
*/
$fields = array(
'name' => 'Full name:',
'birthday' => 'Birthday:',
'house_adress' => 'House adress:',
'post_adress' => 'Post adress:',
'email' => 'Email:',
'phone' => 'Phone mumber:',
'nickname' => 'Forums nickname:',
'shirt' => 'T-shirt size:',
'sticker' => 'Sticker color:'
);
$okMessage = 'Sample success message.';
$errorMessage = 'Sample error message. Try again later.';
$recaptchaSecret = 'xxx-xxxxxxxxxx_xxxxxxxxxx_xxxxxxxxxxxxx';
// let's do the sending
try {
if( !empty( $_POST ) ) {
/*
validate the ReCaptcha, if something is wrong, we throw an Exception,
i.e. code stops executing and goes to catch() block
*/
if ( !isset( $_POST['g-recaptcha-response'] ) ) {
throw new Exception('ReCaptcha is not set.');
}
/*
do not forget to enter your secret key in the config above
from https://www.google.com/recaptcha/admin
*/
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost() );
/*
we validate the ReCaptcha field together with the user's IP address
*/
$response = $recaptcha->verify( $_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR'] );
if( !$response->isSuccess() ) {
throw new Exception('ReCaptcha was not validated.');
}
/*
everything went well, we can compose the message, as usually
*/
$message=array();
$message[]='<html>';
$message[]='<head><title>New Message</title></head>';
$message[]='<body>';
$message[]='<h1>You have new message from contact form</h1>';
foreach( $_POST as $key => $value ) {
if( isset( $fields[ $key ] ) ) {
$message[]=$fields[ $key ] .':'. $value;
}
}
$message[]='IP:'.$_SERVER['REMOTE_ADDR'];
$message[]='</body>';
$from = 'Demo contact form <'.$_POST['email'].'>';
$headers = array(
'Content-Type: text/html; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
$subject='Data form: '.$_POST[ 'name' ];
$status = @mail( $sendTo, $subject, implode( "\r\n", $message ), implode( PHP_EOL, $headers ) );
$response = array(
'type' => 'success',
'message' => $okMessage,
'status' => $status ? 'Message sent' : 'Message Failed'
);
}
} catch ( Exception $e ) {
$response = array( 'type' => 'danger', 'message' => $errorMessage );
}
if ( !empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
$encoded = json_encode( $response );
header('Content-Type: application/json');
exit( $encoded );
} else {
echo $response['message'];
}
?>