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 ;

?>

Ok, so first the XHTML code is missing the <form> label, here's the code

<form action="mail.php" method="post">
   <select name="cboMails">
      <option value="mail1@test.com">mail1@test.com</option>
      <option value="mail2@test.com">mail2@test.com</option>
      <option value="mail3@test.com">mail3@test.com</option>
      <option value="mailN@test.com">mailN@test.com</option>
   </select>
   <input type="submit" value="Submit" />
</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 global variable, in our script we can retrieve the selected mail and send the info to it.
Here is the mail.php file:

<?php
   $to = $_POST['cboMails'];
   $subject = "Test mail";
   $message = "Information to be mailed";
   $from = "mywebsite@mymail.com";
   $headers = "From: $from";
   mail($to,$subject,$message,$headers);
   echo "Mail Sent.";
?>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.