PHP mailsender
HejJeg roder rundt med en php mail sender som, som sender en "thank you" mail til brugeren med det samme...
Jeg kan bare ikke få den til at fungere! Jeg er (stadig ;) ny i Php og vil rigtig gerne have et hint til hvad jeg gør galt! Jeg har revet mig selv ii håret i 30 timer nu...
Håber på hjælp!
Mvh
Oldschool
Nårh ja, koden: (contact.php)
<?php
//Site Options and configuration
//Send Thank you confirmation -> 1=yes,2=no
$SEND_THANKS = 1;
//EMAIL OPTIONS
//Admin emails (contact will be sent to those on the list
$ADMIN_EMAILS = array('anders@hojmose.com');
//Site title
$EMAIL_OPTIONS['TITLE'] = 'mailsender';
//Main email
$EMAIL_OPTIONS['FROM'] = 'anders@hojmose.com';
//Charset
$EMAIL_OPTIONS['CHARSET'] = 'utf-8';
//Type -> HTML=text/html | Text = text/plain
$EMAIL_OPTIONS['TEXT'] = 'text';
//Email subjects
$EMAIL_OPTIONS['USER_SUBJECT'] = 'Thank you for your contact';
$EMAIL_OPTIONS['ADMIN_SUBJECT'] = 'New mail from mailsender';
//EMAIL Template (Sent to User)
$USER_TEMPLATE = 'Hi $+name+$,
We have received your contact from '.$EMAIL_OPTIONS['TITLE'].'
Your message was: $+message_text+$
Reason: $+reason+$
We will respond to you asap to the email you provided: $+email+$
Till then, please review your request and email us back any changed you might have,
We thank you for your contact,
'.$EMAIL_OPTIONS['TITLE'].'';
//Email Template (Sent to Admin)
$ADMIN_TEMPLATE = 'Hi,
You have a new contact @ '.$EMAIL_OPTIONS['TITLE'].'
User name was: $+name+$
User message was: $+message_text+$
User contact reason was: $+reason+$
User email provided: $+email+$
'.$EMAIL_OPTIONS['TITLE'].'';
//Function to check for valid email
function is_valid_email($string) {
return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string);
}
//If contact is being sent:
if($_POST['submit_id'] == 1){
//Check name entered
if($_POST['name'] == NULL){ $message = 'Please enter your name.';}
//check if email is enetered
if($message == NULL && is_valid_email($_POST['email']) == false ){ $message = 'Please enter a valid email.';}
//check if message is entered
if($_POST['message_text'] == NULL && $message == NULL){ $message = 'Please enter a comment.';}
//compose admin/user message templates replaces
$do_search = array('$+name+$','$+email+$','$+message_text+$','$+reason+$');
$do_replace = array($_POST['name'],$_POST['email'],$_POST['message_text'],$_POST['reason']);
//Send user email?
if($SEND_THANKS == 1){
$user_message = str_replace($do_search,$do_replace,$USER_TEMPLATE);
//Set Headers
$user_header = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$user_header .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$user_header .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n";
//Send Thank you
mail ($_POST['email'],$EMAIL_OPTIONS['USER_SUBJECT'],$user_message,$user_header);
}
//Send admin email?
if(count($ADMIN_EMAILS) > 0){
$admin_message = str_replace($do_search,$do_replace,$ADMIN_TEMPLATE);
//regular headers
$headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n";
$new_message = $admin_message;
}
//Send admin emails
foreach($ADMIN_EMAILS as $this_email){
mail ($this_email,$EMAIL_OPTIONS['ADMIN_SUBJECT'],$new_message,$headers);
}
$message = 'Your message has been sent, thank you.';
$_POST = NULL;
if($message != NULL){
?>
<table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FF8080">
<tr>
<td bgcolor="#000000"><font color="#FFFFFF"><?=$message;?></font></td>
</tr>
</table>
<br/>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF?page=contact'];?>" method="post" enctype="multipart/form-data" name="contact" id="contact" style="display:inline;">
<table width="100%" border="0" align="left" cellpadding="5" cellspacing="0">
<tr>
<td>Name:</td>
<td><input name="name" width="170" type="text" id="name" value="<?php echo $_POST['name'];?>"></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" width="170" type="text" id="email" value="<?php echo $_POST['email'];?>"></td>
</tr>
<tr>
<td>Reason for contact: </td>
<td><select name="reason" id="reason" style="width:220px;">
<?php if($_POST['reason'] == 'General dialogue' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
<option value="OPT1"<?=$sel;?>>OPT1</option>
<?php if($_POST['reason'] == 'Order testdrive'){ $sel = ' selected';} else { $sel = NULL;} ?>
<option value="OPT2"<?=$sel;?>>OPT2</option>
<?php if($_POST['reason'] == 'OPT3'){ $sel = ' selected';} else { $sel = NULL;} ?>
<option value="OPT3"<?=$sel;?>>Info on technical specifications</option>
<?php if($_POST['reason'] == 'OPT4'){ $sel = ' selected';} else { $sel = NULL;} ?>
<option value="Other"<?=$sel;?>>OPT4</option>
</select></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="message_text" cols="40" rows="4" id="message_text"><?php echo $_POST['message_text'];?></textarea></td>
</tr>
<tr>
<td></td>
<td colspan="2" align="left">
<input type="submit" name="Submit" value="Send">
<input name="submit_id" type="hidden" id="submit_id" value="1">
</td>
</tr>
</table>
</td>
</tr>
</table> </td>
</tr>
<?php } ?>
</table>
</form>
