942,960 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 445
  • PHP RSS
Sep 1st, 2010
0

Select to send my form to different emails

Expand Post »
I'm having trouble finding help on this specific problem and I'm new so please forgive me.

I'm trying to add a drop-down or checkboxes that allow my client to send the information to a secific email address. I'm trying to get this to work within my existing form and I can't get it to work.


Here is the HTML code:

<select name='emailto' id='emailto'>
<option value='man1@website.com'>man1@website.com</option>
<option value='man2@website.com'>man2@website.com</option>
<option value='man3@website.com'>man3@website.com</option>
<option value='man4@website.com'>man4@website.com</option>
</select>


-------------------------


Here is the PHP Code:
<?php
// ------------- CONFIGURABLE SECTION ------------------------

// $mailto - set to the email address you want the form
// sent to, eg
//$mailto = "youremailaddress@example.com" ;

$mailto = 'corriea@c3softworks.com' ;

// $subject - set to the Subject line of the email, eg
//$subject = "Feedback Form" ;

$subject = "C3 SOFTWORKS -- SUPPORT CALL NOTIFICATION" ;

// the pages to be displayed, eg
//$formurl = "http://www.example.com/feedback.html" ;
//$errorurl = "http://www.example.com/error.html" ;
//$thankyouurl = "http://www.example.com/thankyou.html" ;

$formurl = "form.php" ;
$errorurl = "error.html" ;
$thankyouurl = "thankyou.html" ;
$phone_is_required = 1;
$name_is_required = 1;
$company_is_required = 1;
$email_is_required = 1;
$license_is_required = 0;
$comments_is_required = 0;
$comments2_is_required = 0;
$uself = 0;
$use_envsender = 1;
$use_sendmailfrom = 1;
$use_webmaster_email_for_from = 0;
$use_utf8 = 1;
$my_recaptcha_private_key = '' ;
$email_to = $_POST['emailto'];



// -------------------- END OF CONFIGURABLE SECTION ---------------

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
if (isset( $use_sendmailfrom ) && $use_sendmailfrom) {
ini_set( 'sendmail_from', $mailto );
}
$envsender = "-f$mailto" ;
$phone = (isset($_POST['phone']))? $_POST['phone'] : $_POST['name'] ;
$fullname = (isset($_POST['fullname']))? $_POST['fullname'] : $_POST['name'] ;
$company = (isset($_POST['company']))? $_POST['company'] : $_POST['name'] ;
$email = $_POST['email'] ;
$license = (isset($_POST['license']))? $_POST['license'] : $_POST['name'] ;
$comments = $_POST['comments'] ;
$comments2 = $_POST['comments2'] ;
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (($email_is_required && (empty($email) || !preg_match('/@/', $email))) || ($name_is_required && empty($fullname)) || ($company_is_required && empty($company)) || ($comments_is_required && empty($comments))) {
header( "Location: $errorurl" );
exit ;
}
if ( preg_match( "/[\r\n]/", $fullname ) || preg_match( "/[\r\n]/", $email ) ) {
header( "Location: $errorurl" );
exit ;
}
if (strlen( $my_recaptcha_private_key )) {
require_once( '../Copy of quote/recaptchalib.php' );
$resp = recaptcha_check_answer ( $my_recaptcha_private_key, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] );
if (!$resp->is_valid) {
header( "Location: $errorurl" );
exit ;
}
}
if (empty($email)) {
$email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;

if (function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

$messageproper =
"This message was sent from:\n" .
"$http_referrer\n\n" .
"------------------------------------------------------------\n" .
"Customer Name: $fullname\n" .
"Company Name: $company\n" .
"Phone Number: $phone\n" .
"License Number: $license\n" .
"E-mail Address: $email\n\n" .

"------------------------- What was the issue? -------------------------\n\n\n" .
$comments .
"\n\n\n" .

"------------------------- Additional Comments -------------------------\n\n" .
$comments2 .
"\n\n\n" ;

$headers =
"From: \"$fullname\" <$fromemail>" . $headersep . "Reply-To: \"$fullname\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.15.0" .
$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;

if ($use_envsender) {
mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;

?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
1corrie2 is offline Offline
1 posts
since Sep 2010
Sep 3rd, 2010
0

Basic mailer functionality

Ok, so first the XHTML code is missing the <form> label, here's the code
html Syntax (Toggle Plain Text)
  1. <form action="mail.php" method="post">
  2. <select name="cboMails">
  3. <option value="mail1@test.com">mail1@test.com</option>
  4. <option value="mail2@test.com">mail2@test.com</option>
  5. <option value="mail3@test.com">mail3@test.com</option>
  6. <option value="mailN@test.com">mailN@test.com</option>
  7. </select>
  8. <input type="submit" value="Submit" />
  9. </form>

Quoting W3Schools: "The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts."

This means that we will be able to access the data from any script using the $_POST['cboMails'] global variable, in our script we can retrieve the selected mail and send the info to it.
Here is the mail.php file:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $to = $_POST['cboMails'];
  3. $subject = "Test mail";
  4. $message = "Information to be mailed";
  5. $from = "mywebsite@mymail.com";
  6. $headers = "From: $from";
  7. mail($to,$subject,$message,$headers);
  8. echo "Mail Sent.";
  9. ?>

Notice how we've accessed the selected mail in the global $_POST[] variable (line 2 of the PHP code)

This is basically how to do it, this, however is insecure, since we don't validate any data and malicious users can inject the Cc, Bcc headers to send the information to other mail recipients check this link for secure email
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Using preg_match to grab content between tags
Next Thread in PHP Forum Timeline: validating user input... question?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC