By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

PHP Email Script - Need advice

100+
P: 109
Hello everyone, I'm curious about something. I have a PHP Email script that I'm trying to get working and I think the code is right but just need a second pair of eyes to check it. It seems something in my if elseif else structure is hosed. The site does indeed email properly because I have each request followed by an autoresponder to the message sender. This is the block I'm stuck on:

Expand|Select|Wrap|Line Numbers
  1. if ($to=="sales@mydomain.com")
  2. $subject="RFQ"
  3. mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site RFQ request.")
  4. elseif ($to==webmaster@mydomain.com)
  5. $subject="Website Related";
  6. mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site website request.")
  7. elseif ($to==support@mydomain.com)
  8. mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site support request.")
  9. $subject="Account support request";
  10. else
  11. $subject="Contact Request";
  12. mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site contact request.")
  13.  
Oct 21 '11 #1

✓ answered by Rabbit

Not a problem, we're all new at some point.

If you get around to it, basically you would do something like this
Expand|Select|Wrap|Line Numbers
  1. $arr = array(
  2.    'John Smith' => array(
  3.       'Subject' => 'Hi',
  4.       'EMail' => 'John.Smith@domain.com'
  5.    )
  6.    'Jane Doe' => array(
  7.       'Subject' => 'Hello',
  8.       'EMail' => 'Jane.Doe@gmail.com'
  9.    )
  10. );
  11.  
  12. $arrUser = $arr[$user];
  13. sendMail($arrUser['Subject'], @arrUser['EMail']);

Share this Question
Share on Google+
13 Replies


Rabbit
Expert Mod 10K+
P: 12,359
Your if structure is completely off. You're missing a bunch of brackets and semicolons. The correct if structure is as follows
Expand|Select|Wrap|Line Numbers
  1. if (expression) {
  2.    statement;
  3. } elseif (expression) {
  4.    statement;
  5. } else {
  6.    statement;
  7. }
Oct 21 '11 #2

100+
P: 109
I actually re-wrote this using case statements instead, way faster than if elseif else loop.
Oct 21 '11 #3

100+
P: 109
And here is the re-written code block
Expand|Select|Wrap|Line Numbers
  1. $array = array('1'=>'info@mydomain.com',
  2.                 '2'=>'support@mydomain.com',
  3.                 '3'=>'sales@mydomain.com',
  4.                 '4'=>'webmaster@mydomain.com');
  5.  
  6. if (isset($_POST['Dept']))
  7. {
  8.   $to = $_POST['Dept'];
  9. } else {
  10. $to = 'default';
  11.  
  12. switch ($to) {
  13.   case 1:
  14.   $subject = "General Info Request";
  15.   $mailto = $array['1'];
  16.   break;
  17.  
  18.   case 2:
  19.   $subject = "Account Support Request";
  20.   $mailto = $array['2'];
  21.   break;
  22.  
  23.   case 3:
  24.   $subject = "Request For Quote from $name - $from";
  25.   $mailto = $array['3'];
  26.   mail("1112223333@vtext.com","","RFQ request from $name - $from waiting in your email.");
  27.   break;
  28.  
  29.   case 4:
  30.   $subject = "Website assistance request from $name - $from";
  31.   $mailto = $array['4'];
  32.   break;
  33.  
  34.   default:
  35.   $subject = "No department chosen - to catch-all.";
  36.   $mailto = "webmaster@mydomain.com";
  37.   break
  38. }
  39.  
Oct 21 '11 #4

Rabbit
Expert Mod 10K+
P: 12,359
Another option would be to store all the information in an array and then you can drop the case statements altogether.
Oct 21 '11 #5

100+
P: 109
Hmmm, I may just try that for fun. Right now, I just have to get it working :) Then I can experiment. You'll have to forgive me but I'm a little new to PHP. Most of my experience is in asp.net & C#. I am finding common threads between the languages though.
Oct 21 '11 #6

Rabbit
Expert Mod 10K+
P: 12,359
Not a problem, we're all new at some point.

If you get around to it, basically you would do something like this
Expand|Select|Wrap|Line Numbers
  1. $arr = array(
  2.    'John Smith' => array(
  3.       'Subject' => 'Hi',
  4.       'EMail' => 'John.Smith@domain.com'
  5.    )
  6.    'Jane Doe' => array(
  7.       'Subject' => 'Hello',
  8.       'EMail' => 'Jane.Doe@gmail.com'
  9.    )
  10. );
  11.  
  12. $arrUser = $arr[$user];
  13. sendMail($arrUser['Subject'], @arrUser['EMail']);
Oct 21 '11 #7

100+
P: 109
Ok, I'm trying to code this. The auto-reply to the user works fine but the first part where I send to different departments just doesn't work. Can someone help?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dept=$_REQUEST['Dept'];
  3. $from=$_REQUEST['Email'];
  4. $name=$_REQUEST['Name'];
  5. $headers="From: noreply@mydomain.com";
  6. $array = array('1'=>'info@mydomain.com','2'=>'support@mydomain.com','3'=>'sales@mydomain.com','4'=>'webmaster@mydomain.com');
  7. if (isset($_POST['Dept']))
  8. {
  9.   $to = $_POST['Dept'];
  10. }
  11. else
  12. {
  13. $to = 'default';
  14. }
  15. switch ($to)
  16. {
  17.   case 1:
  18.   $subject = "General Info Request";
  19.   $mailto = $array['1'];
  20.   break;
  21.   case 2:
  22.   $subject = "Account Support Request";
  23.   $mailto = $array['2'];
  24.   break;
  25.   case 3:
  26.   $subject = "Request For Quote from $name - $from";
  27.   $mailto = $array['3'];
  28.   mail("1112223333@mytext.com","","RFQ request from $name - $from waiting in your email.");
  29.   break;
  30.   case 4:
  31.   $subject = "Website assistance request from $name - $from";
  32.   $mailto = $array['4'];
  33.   break;
  34.   default:
  35.   $subject = "No department chosen - to catch-all.";
  36.   $mailto = "webmaster@mydomain.com";
  37.   break;
  38. }
  39. $fields=array();
  40. $fields{"Name"}="Name";
  41. $fields{"Company"}="Company";
  42. $fields{"Email"}="Email";
  43. $fields{"Phone"}="Phone";
  44. $fields{"list"}="Mailing List";
  45. $fields{"Message"}="Message";
  46. $body="We have received the following information:\n\n"; 
  47. foreach($fields as $a => $b)
  48. {
  49. $body=sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
  50. }
  51. $headers2="From: noreply@mydomain.com";
  52. $subject2="Thank you for contacting us";
  53. $autoreply="Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours. If you have any more questions, please go to our website at www.mydomain.com";
  54. if($from=='')
  55.     {
  56. print "You have not entered an email, please go back and try again";
  57.     }
  58. else
  59.     {
  60.     if($name=='')
  61.     {
  62.         print "You have not entered a name, please go back and try again";
  63.     }
  64.     else
  65.     {
  66.         $send=mail($mailto, $subject, $body, $headers);
  67.         $send2=mail($from, $subject2, $autoreply, $headers2);
  68.         if($send)
  69.         {
  70.             header("Location:http://www.mydomain.com/prototype/thankyou.html");
  71.         }
  72.         else
  73.         {
  74.             print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
  75.         }
  76.     }
  77. }
  78. ?>
  79.  
Oct 21 '11 #8

Rabbit
Expert Mod 10K+
P: 12,359
It doesn't even send to default?
Oct 22 '11 #9

100+
P: 109
Nope, I just tried and no dice.
Oct 22 '11 #10

100+
P: 109
Actually, sales & support work. The info and webmaster one don't.
Oct 22 '11 #11

100+
P: 109
SO it would appear that case 1 & 4 do not work.
Oct 22 '11 #12

P: 7
Here is another way you could set this up which I prefer but I know its a personal choice.

Also, I comments 2 parts that im not sure of.

#1: what are these fields supposed to be? are they supposed to be coming from the POST data from the form?

#2: the original foreach loop, overwrote $body with each iteration, im not sure why you want to loop through the fields but if you do and want to append to the $body variable, use " .= " instead of just " = ".



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dept = $_REQUEST['Dept'];
  3. $from = $_REQUEST['Email'];
  4. $name = $_REQUEST['Name'];
  5.  
  6. $headers = "From: noreply@mydomain.com";
  7.  
  8. $mail_settings = array(
  9.                        '1' => array(
  10.                                 'to' => 'info@mydomain.com',
  11.                                 'subject' => 'General Info Request'
  12.                                 ),
  13.                        '2' => array(
  14.                                 'to' => 'support@mydomain.com',
  15.                                 'subject' => 'Account Support Request'
  16.                                 ),
  17.                        '3' => array(
  18.                                 'to' => 'sales@mydomain.com',
  19.                                 'subject' => "Request For Quote from $name - $from"
  20.                                 ),
  21.                        '4' => array(
  22.                                 'to' => 'webmaster@mydomain.com',
  23.                                 'subject' => "Website assistance request from $name - $from"
  24.                                 ),
  25.                         'default' => array(
  26.                                 'to' => 'webmaster@mydomain.com',
  27.                                 'subject' => 'No department chosen - to catch-all.'                                
  28.                                 )                                
  29.                        );
  30.  
  31.  
  32. $to = (isset($_POST['Dept'])) ? $_POST['Dept'] : 'default';
  33.  
  34. $subject = $mail_settings[$to]['subject'];
  35. $mailto  = $mail_settings[$to]['to'];
  36.  
  37. $fields = array(); //#1
  38. $fields["Name"]    = "Name";
  39. $fields["Company"] = "Company";
  40. $fields["Email"]   = "Email";
  41. $fields["Phone"]   = "Phone";
  42. $fields["list"]    = "Mailing List";
  43. $fields["Message"] = "Message";
  44.  
  45. $body = "We have received the following information:\n\n"; 
  46.  
  47. foreach($fields as $a => $b){
  48.  
  49.     $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);  //#2
  50.  
  51. }
  52.  
  53. $headers2  = "From: noreply@mydomain.com";
  54. $subject2  = "Thank you for contacting us";
  55. $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours. ".
  56.              "If you have any more questions, please go to our website at www.mydomain.com";
  57.  
  58. if($from == ''){
  59.  
  60.     print "You have not entered an email, please go back and try again";
  61.  
  62. }else{
  63.  
  64.     if($name == ''){
  65.  
  66.         print "You have not entered a name, please go back and try again";
  67.  
  68.     }else{
  69.  
  70.         $send  = mail($mailto, $subject, $body, $headers);
  71.         $send2 = mail($from, $subject2, $autoreply, $headers2);
  72.  
  73.         if($send){
  74.  
  75.             header("Location:http://www.mydomain.com/prototype/thankyou.html");
  76.  
  77.         }else{
  78.  
  79.             print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
  80.  
  81.         }
  82.     }
  83. }
  84. ?>
  85.  
Oct 23 '11 #13

100+
P: 109
Its ok, I rewrote it and have it working now. I stripped out the foreach loop and just wrote the body out into the $body tag. Thanks :)
Oct 24 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.