Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending php email to two addresses 1

Status
Not open for further replies.

jasboy2005

Programmer
Jan 18, 2005
9
GB
Hi there,

I have a form that users complete. I want to give them options where to send it to. There are two check boxes each with a designated email address value. when the form is submitted to the php script, it needs to know where to send the info to (GetEmailAddress1 and/or GetEmailAddress2).

my question is how do i add a second email address into this line?


mail($GetEmailAddress1,$subject,$text,"From: $GetFirstName <$email>");


many thanks in advance

jasboy
 
Here's one way:
Code:
$to = array();
if (isset($_POST['GetEmailAddress1'])) $to[] = stripslashes($_POST['GetEmailAddress1']);
if (isset($_POST['GetEmailAddress2'])) $to[] = stripslashes($_POST['GetEmailAddress2']);
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
First you build a temporary array consisting of either or both addresses. The 'implode()' function will create a comma separated string in the variable '$to_addr' which you feed to the 'mail()' function.

Ken
 
thx for your reply sleipnir214,

this works if you want to send it to two recipients
i.e. both email checkboxes ticked.

...but it doesn't work if only one checkbox is ticked, because it doesn't recognise a blank email address for the unchecked email checkbox.

therefore, the email will probably be sent to the recognised email addy, but wil also return an error saying that it doesn't recognise the second (blank) recipient.

cheers for any help.

jasboy
 
This is what i did, but i got a parse error:

Code:
<?php
@extract($_POST);
$name = ("$GetFirstName $GetSurname");
$email = stripslashes($GetEmail);
$subject = ("Email From Website");
$text = stripslashes("Hi there,\n\n\I would like to register, so here are my details:\n\nName: $GetTitle $GetFirstName $GetSurname\nAddress: $GetAddress1 $GetAddress2, $GetTown, $GetCounty, $GetPostcode\nTelephone: $GetTel\nMobile: $GetMob\nMany thanks.");
$to = array();
if (isset($_POST['$Getemailaddress1'])) $to[] = stripslashes($_POST['Getemailaddress1']);
if (isset($_POST['$Getemailaddress2'])) $to[] = stripslashes($_POST['Getemailaddress2']);
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
?>

... am i making a mistake here?

cheers

jasboy
 
Try this:
Code:
if (isset($_POST['GetEmailAddress1']) || isset($_POST['GetEmailAddress2']))
{
  if (isset($_POST['GetEmailAddress1']))
  {
    if (isset($_POST['GetEmailAddress2']))
    {
      $to = array();
      $to[] = stripslashes($_POST['GetEmailAddress1']);
      $to[] = stripslashes($_POST['GetEMailAddress2']);
      $to_addr = implode(',',$to);
    }
    else
    {
      $to_addr = stripslashes($_POST['GetEmailAddress1']);
    }
  }
  else
  {
    $to_addr = stripslashes($_POST['GetEmailAddress2']);
  }
}

The first thing it does is check to make sure one of them is checked. Then, if the first one is checked, it sees if the second one is also checked. If the first one is checked and the second one is checked, it implodes them into a comma-separated string and sets $to_addr. If the first one is checked and the second one is not, it sets the $to_addr to the first one. If the first one isn't checked, but one of them is definitely checked, then it sets $to_addr to the second address.

I haven't checked to make sure the code works, but the logic is pretty sound... I think. :)

Marc
 
Where did you get the parse error?

I wouldn't use the extract() function. I used to, but stopped and now I just use $_POST['var'] or $_GET['var'] depending on the context.

A comment to Marc: My code does the same as your's with far fewer steps and tests. Try it with a short program and you'll see what happens in all conditions. It might be overkill with just two choices, but it is easily extended to work with any number of choices.

Ken
 
Ken: I wasn't sure what the implode() would do with only one item in the array, and I think I misread what jasboy said to mean that he actually had tried it and gotten an error - and thus assumed that implode() tacked on a comma and that was causing a problem or something. I like your way better, as long as the implode() doesn't wig out with only one item in the array to be imploded - much easier, and, as you said, mine would be nightmarish if there were more than two items to check off. :)

Marc
 
Implode() "does the right thing" with any array of one element. No trailing delimiter.

I've used it in many situations, including creating update queries in MySQL.

Ken
 
One minor fix to my code (I decided to test what I wrote :))
Code:
$to = array();
if ($_POST['GetEmailAddress1'] != '') $to[] = stripslashes($_POST['GetEmailAddress1']);
if ($_POST['GetEmailAddress2'] != '') $to[] = stripslashes($_POST['GetEmailAddress2']);
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
Reason: a text box will always send something. If you leave it blank, that something will be a zero length string.

Ken
 
hi there again.

ok guys...
i am using the code below on the thankyou.php return page. all the values are entered from a form on another page.

...but i still get a parse error:
Code:
Parse error: parse error, unexpected T_VARIABLE in blah blah blah.. on line 11
... which is this line
Code:
$to_addr = implode(',',$to);

Code:
<?php
@extract($_POST);
$name = ("$GetFirstName $GetSurname");
$email = stripslashes($GetEmail);
$subject = ("Email From Website");
$text = stripslashes("Hi there,\n\n\I would like to register, so here are my details:\n\nName: $GetTitle $GetFirstName $GetSurname\nAddress: $GetAddress1 $GetAddress2, $GetTown, $GetCounty, $GetPostcode\nTelephone: $GetTel\nMobile: $GetMob\nProperty Texts (blank = No): $Gettext\nEmail: $GetEmail\n\n\n\nMany thanks.");

$to = array();
if ($_POST['GetEmail1'] != '') $to[] = stripslashes($_POST['GetEmail1']);
if ($_POST['GetEmail2'] != '') $to[] = stripslashes($_POST['GetEmail2']);
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
?>

i took a look at the form at (and thank you for this), but i need to send more than the subject and message. i need to send about 12 values from the form.

any thoughts very much appreciated.

cheers, jasboy
 
Sometimes PHP gets confused when it comes to writing out an error message. It will continue to look at input until it can't figure out what you want (or what it thinks you want) and then issue the error.

Looking at your code:
Why do you have the "(" and ")" surrounding on these lines:
Code:
$name = ("$GetFirstName $GetSurname");
Code:
$subject = ("Email From Website");
They can be written:
Code:
$name = $GetFirstName . ' ' . $GetSurname;
$subject = 'Email From Website';
As for getting all of the fields into your outgoing message, just add them to the body of the message. For example:
Code:
$body = 'Field 1: '.$field1. "\n";
$body .= 'Field 2: '.$field2. "\n";
// ... etc ...
Ken
 
Nice examples Ken, I learned a few new things I never really understood how implode worked for one! Thanks.
 
thx for all the attention.

so just to clarify, are you saying that this script from the thankyou page will work, or do i need to do alternative things....

Code:
<?php
@extract($_POST);
$name = $GetFirstName . ' ' . $GetSurname;
$email = stripslashes($GetEmail);
$subject = 'Email From Website';
$text = stripslashes("Hi there,\n\n\I would like to register, so here are my details:\n\nName: $GetTitle $GetFirstName $GetSurname\nAddress: $GetAddress1 $GetAddress2, $GetTown, $GetCounty, $GetPostcode\nTelephone: $GetTel\nMobile: $GetMob\nEmail: $GetEmail\n\n\n\nMany thanks.");

$to = array();
if ($_POST['GetEmail1'] != '') $to[] = stripslashes($_POST['GetEmail1']);
if ($_POST['GetEmail2'] != '') $to[] = stripslashes($_POST['GetEmail2']);
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
?>

thx guys for all your help

jasboy
 
i just tried this script, but i still get the parse error. it just doesn't seem to recognise the values. remember that the form resides on a different page from the actual php script. maybe i need to start again with the form page and the thankyou page. has anyone got time to knock me these up. just a simple form with say three values: name, address, telephone, email address1 (checkbox), email address2 (checkbox). when this is sent, it goes to a thankyou page. i really appreciate all your help here, but i just can't seem to get it to work within the realms of my own pages.

many many thanks guys, very much appreciated.

cheers, jasboy
 
hi guys,

still having problems getting this thing to work. to make things a little clearer i have uploaded the form i will be using along with the code for the target page. hope you can help me out??!!



cheers

jasboy
 
i have had a look at other sites that have thing functionality so it shouldn't be a problem to do. i'm pretty sure i'm almost there, so i don't wanna give up on this just yet. any thoughts guys are much appreciated.

cheers,

jasboy
 
I think I see part of your problem. When I wrote:
Code:
if (!empty($to)) {
 $to_addr = implode(',',$to);
 mail($to_addr,...); }
I meant for you to put the rest of your parameters to the mail() function in place of the "..."

The "..." is just a placeholder, not something to be taken literally.

Ken
 
Thank you Ken!!!!!
Sorry about that, i was just being very thick!
It all works now.

Thank you to everyone who helped me out on this one.

cheers, jasboy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top