Forum Moderators: coopster

Message Too Old, No Replies

mail form using php

sending more than one email using drop down list

         

ianb

11:49 am on May 16, 2005 (gmt 0)

10+ Year Member



I'm trying to send more than one email per option choose. when software is chosen it will pass an array of different email addresses and send it to them.

e.g. when software is selected, then the email will be send to a number of addresses associated with that option.

Here's what i have so far, which just sends one email. can anyone please show me how to solve my problem. thanks!
-------------------------------------------------
-
-
Code:

<td><div align="left">
<select name="typesupport" id="typesupport">
<option selected>--select type--</option>
<option value="hardware">hardware</option>
<option value="software">software</option>
</select>
*</div></td>
-

Thanks!
-

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

PHP Code:
<?php
/*Here we are going to declare the variables*/
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$location = $_POST['location'];
$contactnum = $_POST['contactnum'];
$typesupport = $_POST['typesupport'];
$ipaddress = $_POST['ipaddress'];
$probdesc = $_POST['probdesc'];
$hsolved = $_POST['hsolved'];

//Save visitor name and entered message into one variable:

$subject = "log problem with example";
$formcontent="PROBLEM LOGGED BY ------- $name\n\nCLIENT NAME ------- $company\n\nLOCATION ------- $location\n\nCONTACT NUMBER ------- $contactnum\n\n
TYPE OF SUPPORT ------- $typesupport\n\nIP ADDRESS ------- $ipaddress\n\nDESCRIPTION OF PROBLEM ------- $probdesc\n\nHAS THIS PROBLEM BEEN SOLVED BEFORE? ------- $hsolved";

/*mail header*/
$mailheader = "From: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";

$valid_emails = array("some.person","another.person");

if(in_array($_POST['typesupport'],$valid_emails))
{
$recipient = $_POST['typesupport'] . "@example.co.uk";

mail($recipient, $subject, $formcontent, $mailheader);
echo "mail has been sent";

}
else
{
echo "problems sending the email. Have a word!";
}

?>

[edited by: jatar_k at 4:33 pm (utc) on May 16, 2005]
[edit reason] generalized names and domains [/edit]

StupidScript

10:28 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding another array containing the arrays you want to check and building your mailto list from within those arrays:

<form method=post>

<select name="typesupport">

<option />hardware

<option />software

</select>

<input type="submit">

</form>

<?

$sendto="";

$hard_emails = array("hard.guy","hard.gal");

$soft_emails = array("soft.guy","soft.gal");

$addrs = array("hardware"=>$hard_emails,"software"=>$soft_emails);

if ($_POST["typesupport"]) {

foreach($addrs as $key => $val) {

if ($_POST["typesupport"]==$key) {

for($i=0;$i<sizeof($val);$i++) {

$sendto.=$val[$i]."@some.uk,";

}

}

}

#REMOVE EXTRA COMMA AT THE END

$sendto=substr($sendto,0,-1);

#FOR VIEWING RESULT (COPY-AND-PASTE THIS ENTIRE CODE TO TEST)

echo "SEND TO: ".$sendto."<br />\n";

}

?>

Voila.

ianb

10:05 am on May 19, 2005 (gmt 0)

10+ Year Member



thanks for that. undestand a bit better

i've got a bit further. can anyone help me? thanks
--------------------------------------------
------------
logproblem.php

<select name="typesupport" id="typesupport">
<option selected>--select type--</option>
<option value="hardware">hardware</option>
<option value="software">software</option>
</select>

--------
logproblem_success.php
[PHP]<?php
/*Here we are going to declare the variables*/
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$location = $_POST['location'];
$contactnum = $_POST['contactnum'];
$typesupport = $_POST['typesupport'];
$ipaddress = $_POST['ipaddress'];
$probdesc = $_POST['probdesc'];
$hsolved = $_POST['hsolved'];

//Save visitor name and entered message into one variable:

$subject = "log problem with microcomputing";
$formcontent="PROBLEM LOGGED BY ------- $name\n\nCLIENT NAME ------- $company\n\nLOCATION ------- $location\n\nCONTACT NUMBER ------- $contactnum\n\n
TYPE OF SUPPORT ------- $typesupport\n\nIP ADDRESS ------- $ipaddress\n\nDESCRIPTION OF PROBLEM ------- $probdesc\n\nHAS THIS PROBLEM BEEN SOLVED BEFORE? ------- $hsolved";

/*mail header*/
$mailheader = "From: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";

$hard_emails = array("name1","name2");
$soft_emails = array("name1","name2");
$addrs = array("hardware"=>$hard_emails,"software"=>$soft_emails);
$confirmed_emails = array();

if ($_POST["typesupport"])
{
foreach($addrs as $key => $val)
{
if($_POST["typesupport"]==$key)
{
for($i=0;$i<sizeof($val);$i++)
{
$confirmed_emails[] = $_POST['typesupport'].'@example.co.uk';
}
}
}
}

if(!empty($confrimed_emails))
{
$recipient = implode (',', $confrimed_emails);
mail($recipient, $subject, $formcontent, $mailheader);
echo "mail has been sent";
}
else
{
echo "problems sending the email.";
}

php>[/PHP]

[edited by: jatar_k at 4:17 pm (utc) on May 19, 2005]
[edit reason] generalized emails [/edit]

StupidScript

5:10 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First off, you do not need to make an array out of the confirmed_emails, you can just keep it as a string.

$confirmed_emails .= $val[$i]."@example.co.uk,";

and AFTER the loop has completed:

#strip off the trailing comma:

$confirmed_emails = substr($confirmed_emails,0,-1);

then just use $confirmed_emails instead of needing to implode the array.

Second, if you copy-and-pasted, there's a spelling issue with your mail() attempt:

if(!empty($[b]confrimed[/b]_emails)) 

{ 

#just use $confirmed_emails instead of $recipient

$recipient = implode (',', $[b]confrimed[/b]_emails); 

mail($recipient, $subject, $formcontent, $mailheader); 

echo "mail has been sent"; 

}

ianb

11:45 am on May 20, 2005 (gmt 0)

10+ Year Member



i've changed what you've said and one or two other bits and it seems to be working now.

thanks alot for all your help!