Jump to content

Recommended Posts

Hi all,

I’m trying to build a contact form and I need the destination email to be dynamic...

This is what I got...

/* Admin emails */
$adminEmails = array('mail1@mailbox.pt', 'mail2@mailbox.pt'); 
/* Site Title */
$siteTitle = 'Demo Contact Form';
/* Email From */
$emailFrom = 'geral@mailbox.pt';

 

With this everything is working fine but I need the admin emails to assume a variable value...

This...

 

$myvar1  =  'mail1@mailbox.pt';
$myvar2 =  'mail2@mailbox.pt';

/* Admin emails */
$adminEmails = array($myvar1, $myvar2); 
/* Site Title */
$siteTitle = 'Demo Contact Form';
/* Email From */
$emailFrom = 'geral@mailbox.pt';

 

But This does not work...

Someone can help?

Sorry my English

 

This is the full script i'm using

http://www.lightphpscripts.com/?s=Free-PHP-Contact-Form

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/224441-dynamic-email-recipient-box/
Share on other sites

If you mean "dynamic" as in "live search" (think: google - updates as you type), you'll most likely want a PHP/AJAX/MySQL solution. Google "live search php", and you'll get a handful of tutorials. I have very limited experience with AJAX, and it took a few tries, but it's not too hard to do. If you're good with AJAX/JavaScript, it'll probably be a lot easier.

 

On the other hand, if you mean 'dynamic', in the sense that clicking "Email Us" from the tech support page inserts "techsupport@domain.com", while clicking "Email Us" from the sales page inserts "sales@domain.com", that's a pretty easy chunk of PHP code.

 

Lastly, and I've only ever needed this once, if you want a "dynamic" feature where there are checkboxes (Sales, Support, Billing), and you want the email addresses updated with the proper email addresses depending on the checkboxes that are checked, that can be done relatively easy with JS.

First of all thanks for the replies.

 

When I use an email address everything works fine and the mail is sent... For example:

$adminEmails = array('mailbox1@geral.pt');

But if I replace the mailbox with a variable it gives me an error...  For example:

$gvarsemail  = 'mailbox1@geral.pt';
$adminEmails = array($gvarsemail);

This is the error I receive:

Warning: mail() [function.mail]: SMTP server response: 503 5.5.2 Need rcpt command in …\contact.php on line 126

 

 

 

This is the full script i'm using...



<?php
$gvars = $_SERVER['QUERY_STRING'];
$gvarscln = explode("&", $gvars);
$gvarsemail =  $gvarscln[0];

/* Do you want to send a thank you email */
$sendThankYou = TRUE;

/* Require mental question challenge */
$mentalQuestion = TRUE;

/* Mental question list - the more, the
better secured you are from auto bots */

$mentalQuestionList[] = array("What is the square root of 169?","13");
$mentalQuestionList[] = array("What is the 1.3 divided by .2?","6.5");
$mentalQuestionList[] = array("What is the color of a green fire?","green");
$mentalQuestionList[] = array("LightPHPScripts rocks,true/false?","false");
$mentalQuestionList[] = array("2532 + 5328","7860");


/* Email Options */
//-----------------
/* Admin emails */
$adminEmails = array($gvarsemail); 



/* Site Title */
$siteTitle = 'Demo Contact Form';

/* Email From */
$emailFrom = 'multimedia@guardadigital.pt';

/* Email Charset */
$emailCharset = 'utf-8';

/* Email Title */
$emailTitle = 'Thank you for your contact';

/* Admin Email Title */
$emailAdminTitle = 'You have received a new contact';


/*
For Templates you can use the following variables:
$+name+$ -> User Name Sending Contact
$+email+$ -> User Email Sending Contact
$+message_text+$ -> Contact Message
$+reason+$ -> Reason For Contact
*/

/* Email Subject */
$emailSubject = '<b>Hi $+name+$,</b></span><br /><br />
<span align="justify"><b>We have received your contact from '.$siteTitle.'.</b></span><br /><br />
<span align="justify"><b>Your message was: <font color="blue">$+message_text+$</font></b></span><br /><br />
<span align="justify"><b>Reason: <font color="blue">$+reason+$</font></b></span><br /><br />
<span align="justify"><b>We will respond to you asap to the email you provided <font color="blue">$+email+$</font></b></span><br /><br />
<span align="justify"><b>Till then, please review your request and email us back any changed you might have, </b></span><br /><br />
<span align="justify"><b>We thank you for your contact, </b></span><br /><br /><br />
<span align="justify"><b>'.$siteTitle.'</b></span><br />';

/* Admin Email Subject */
$emailSubjectAdmin = '<b>Hi Admin,</b></span><br /><br />
<span align="justify"><b>You have a new contact @ '.$siteTitle.'.</b></span><br /><br />
<span align="justify"><b>User name was: <font color="blue">$+name+$</font></b></span><br /><br />
<span align="justify"><b>User message was: <font color="blue">$+message_text+$</font></b></span><br /><br />
<span align="justify"><b>User contact reason was: <font color="blue">$+reason+$</font></b></span><br /><br />
<span align="justify"><b>User email provided <font color="blue">$+email+$</font></b></span><br /><br />
<span align="justify"><b>'.$siteTitle.'</b></span><br />';

/* End Configurations */

//Function to check for valid email
function is_valid_email($string) {
return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string);
}


/** SEC 334 **/


/* Are we submitting form */
if($_POST['submitID'] == 1){

/* Check user entered variables */
if($_POST['name'] == NULL){ $message = 'Please enter your name.';}
if($message == NULL && is_valid_email($_POST['email']) == false ){ $message = 'Please enter a valid email.';}
if($_POST['messageText'] == NULL && $message == NULL){ $message = 'Please enter a comment.';}	

/* Check Mental Question */
if($mentalQuestion == TRUE)
{
	foreach($mentalQuestionList as $question) {if($question[0] == $_POST['question']){$answer = $question[1];}}
	if($answer != $_POST['finalAnswer'] || $answer == NULL)
	{
		$message = 'Your answer to "'.$_POST['question'].'" is invalid.';
	}
}
/* Actual sending */
if($message == NULL){

	/* Compose messages */
	$doSearch = array('$+name+$','$+email+$','$+message_text+$','$+reason+$');
	$doReplace = array($_POST['name'],$_POST['email'],$_POST['messageText'],$_POST['reason']);

	/* Compose headers */
	$headers = "Return-Path: ".$siteTitle." <".$emailFrom.">\r\n"; 
	$headers .= "From: ".$siteTitle." <".$emailFrom.">\r\n";
	$headers .= "Content-Type: text/html; charset=".$emailCharset.";\n\n\r\n"; 

	/* Send Thank you Email */
	if($sendThankYou == TRUE){
		$userMessage = str_replace($doSearch,$doReplace,$emailSubject);
		//Send Thank you
		mail ($_POST['email'],$emailTitle,$userMessage,$headers);	
	}

	$adminMessage = str_replace($doSearch,$doReplace,$emailSubjectAdmin);

	/* Send Admin Emails */
	if(count($adminEmails) > 0){			
		foreach($adminEmails as $thisEmail){
		/*Line 126*/	mail($thisEmail,$emailAdminTitle,$adminMessage,$headers);	
		}
	}

	$message = 'Your contact has been sent, thank you.';
	$_POST = NULL;
}
}
if($message != NULL){
?>
<table width="100%"  border="0" cellpadding="5" cellspacing="0" bgcolor="#FF8080">
  <tr>
    <td bgcolor="#FFD5D5"><font color="#FF0000"><?=$message;?></font></td>
  </tr>
</table>
<br/>
<?php } ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" 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" type="text" id="name" value="<?php echo $_POST['name'];?>"></td>
</tr>
<tr>
  <td>Email:</td>
	<td><input name="email" 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:154px;">
	<?php if($_POST['reason'] == 'Support' || $_POST['reason'] == NULL){ $sel = ' selected';} else { $sel = NULL;} ?>
	<option value="Support"<?=$sel;?>>Support</option>
	<?php if($_POST['reason'] == 'Billing'){ $sel = ' selected';} else { $sel = NULL;} ?>
	<option value="Billing"<?=$sel;?>>Billing</option>
	<?php if($_POST['reason'] == 'Complaints'){ $sel = ' selected';} else { $sel = NULL;} ?>
	<option value="Complaints"<?=$sel;?>>Complaints</option>
	<?php if($_POST['reason'] == 'Other'){ $sel = ' selected';} else { $sel = NULL;} ?>
	<option value="Other<?=$sel;?>">Other</option>
  </select></td>
</tr>
<tr>	
  <td>Message:</td>
	<td><textarea name="messageText" cols="40" rows="4" id="messageText"><?php echo $_POST['messageText'];?></textarea></td>
</tr>
<?php
if($mentalQuestion == TRUE)
{
	$random = rand(0,count($mentalQuestionList)-1);
	$question = $mentalQuestionList[$random][0];
?>
<tr>
  <td><?php echo $question;?></td>
	<td>		
	  <input name="finalAnswer" type="text" id="finalAnswer" value="<?php echo $_POST['finalAnswer'];?>" /></td>
</tr>
<?php
}
?>
<tr>
	<td colspan="2"><div align="center">
	<input type="submit" name="Submit" value="Send Contact">
	<input type="hidden" id="question" name="question" value="<?php echo $question;?>">
	<input name="submitID" type="hidden" id="submitID" value="1">
	</div></td>
</tr>
<tr>
  <td colspan="2"><div align="right"><a href="http://www.LightPHPScripts.com" target="_blank"><font size="1">Powered By LPS</font></a></div></td>
</tr>
</table>     
</form>
<!--
/*
This script was downloaded at:
LightPHPScripts.com
Please support us by visiting
out website and letting people
know of it.
Produced under: LGPL
*/
-->

 

Once again... Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.