我正在更新我的帖子,因为我还没有用我的新代码解决它,使用切换功能。我希望用户能够从下拉联系人中进行选择,并且该选择也会设置主题。截至目前,我一直是默认而不是案例1或2。
<?php
//PHP Mail Form
function sanstr($value)
{
$value = strip_tags($value);
$value = trim($value);
$value = escapeshellcmd($value);
$value = htmlentities($value);
return $value;
}
$errorMessage = array();
switch ($to)
{
case "1":
$to = "example1@example.com";
$subject = "Subject 1";
break;
case "2":
$to = "example2@example.com";
$subject = "Subject 2";
break;
default:
$to = "example@example.com";
$subject = "Default";
break;
{
if(isset($_POST['form']))
{
$formData = $_POST['form'];
if (filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
$from = sanitizeString($formData['from']);
}
else
{
$errorMessage[] = "Please use a valid email format: name@domain.com";
}
if(strlen($formData['name']) > 0)
{
$name = sanitizeString($formData['name']);
}
else
{
$errorMessage[] = "Please enter your name.";
}
if(strlen($formData['title']) > 0)
{
$title = sanitizeString($formData['title']);
}
else
{
$title = '';
}
if(strlen($formData['company']) > 0)
{
$company = sanitizeString($formData['company']);
}
else
{
$company = '';
}
if(strlen($formData['phone']) > 0)
{
$phone = sanitizeString($formData['phone']);
}
else
{
$errorMessage[] = "Please enter a phone number.";
}
if(strlen($formData['message']) > 0)
{
$message = sanitizeString($formData['message']);
}
else
{
$errorMessage[] = 'Cannot leave message box blank.';
}
if (empty($errorMessage))
{
$email_headers = "From:" . $from . "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
$message_send = "<h3>" . $name . "<br>" . $title . "<br>" . $company . "<br>" . $phone . "<br>" . $from . "</h3><hr><h4>" . $subject . "</h4>" . $message;
if (mail($to, $subject, $message_send, $email_headers))
{
$errorMessage[] = 'Thank you, your email is on the way!';
}
else
{
$errorMessage[] = 'There seems to be a problem!';
}
}
}
?>
答案 0 :(得分:0)
以下是if / elseif / else语句的返工,是您问题中的第二个代码块:
if (strlen($formData['subject']) > 0)
{
$subject = sanitizeString($formData['subject']);
}
elseif ($receivers == "1")
{
$subject = "Subject 1";
}
elseif ($receivers == "2")
{
$subject = "Subject 2";
}
else
{
$subject = "Default";
}
如果没有满足任何条件,我添加了默认值。在这种情况下,您还可以查看 Switch 语句,如果您有多个备用项,则更有效。希望这会有所帮助。
答案 1 :(得分:0)
我收到了这个问题的帮助。如果有人需要这样的话,我发布了解决问题的代码。
<?php
//Sainitize function
function sanitizeString($value){
$value = strip_tags($value);
$value = trim($value);
$value = escapeshellcmd($value);
$value = htmlentities($value);
return $value;
}
//Receivers
$receivers = array();
//IF built with query put email as key, subject as value in loop or specify like below
$receivers['default@example.com'] = 'Default Subject';
$receivers['example1@example.com'] = 'Subject 1';
$receivers['example2@example.com'] = 'Subject 2';
$errorMessage = array();
if(isset($_POST['form'])){
$formData = $_POST['form'];
if (filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
$from = sanitizeString($formData['from']);
}else{
$errorMessage[] = "Please use a valid email format: name@domain.com";
}
if(strlen($formData['to']) > 0){
$to = sanitizeString($formData['to']);
}else{
$to = 'example@example.com';
}
if(strlen($formData['name']) > 0){
$name = sanitizeString($formData['name']);
}else{
$errorMessage[] = "Please enter your name.";
}
if(strlen($formData['title']) > 0){
$title = sanitizeString($formData['title']);
}else{
$title = '';
}
if(strlen($formData['company']) > 0){
$company = sanitizeString($formData['company']);
}else{
$company = '';
}
if(strlen($formData['phone']) > 0){
$phone = sanitizeString($formData['phone']);
}else{
$errorMessage[] = "Please enter a phone number.";
}
if(strlen($formData['message']) > 0){
$message = sanitizeString($formData['message']);
}else{
$errorMessage[] = 'Cannot leave message box blank.';
}
if (empty($errorMessage) && $formData['message']){
if(strlen($formData['subject']) > 0){
$subject = sanitizeString($formData['subject']);
}else{
$subject = $receivers[$to];
}
$email_headers = "From:" . $from . "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
$message_send = "<h3>" . $name . "<br>" . $title . "<br>" . $company . "<br>" . $phone . "<br>" . $from . "</h3><hr><h4>" . $subject . "</h4>" . $message;
if (mail($to, $subject, $message_send, $email_headers)){
$errorMessage[] = 'Thank you, your email is on the way!';
}else{
$errorMessage[] = 'There seems to be a problem!';
}
}
}
?>
感谢所有试图提供帮助的人!