473,414 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

PHP Email Script - Need advice

dbrewerton
115 100+
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']);

13 1753
Rabbit
12,516 Expert Mod 8TB
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
dbrewerton
115 100+
I actually re-wrote this using case statements instead, way faster than if elseif else loop.
Oct 21 '11 #3
dbrewerton
115 100+
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
12,516 Expert Mod 8TB
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
dbrewerton
115 100+
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
12,516 Expert Mod 8TB
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
dbrewerton
115 100+
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
12,516 Expert Mod 8TB
It doesn't even send to default?
Oct 22 '11 #9
dbrewerton
115 100+
Nope, I just tried and no dice.
Oct 22 '11 #10
dbrewerton
115 100+
Actually, sales & support work. The info and webmaster one don't.
Oct 22 '11 #11
dbrewerton
115 100+
SO it would appear that case 1 & 4 do not work.
Oct 22 '11 #12
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
dbrewerton
115 100+
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

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

Similar topics

1
by: Chris Lane | last post by:
Need Advice on prebuilt Exception Assemblies Please take a look at my post on the Titled: Need Advice on prebuilt Exception Assemblies posted on 04/21/04 Thank
3
by: hazly | last post by:
I'm very new in the web technology and need advice on search engine. I want to develop a portal using PHP and MySQL on Linux. Need to know on the following features : 1. search engine that could...
4
by: wayne | last post by:
I have a PHP email script running on two separate websites. Today, I received a form mail generated by the script from each site, with time stamps 10 minutes apart. The entered email address in...
9
by: Jerim79 | last post by:
I am no PHP programmer. At my current job I made it known that I was no PHP programmer during the interview. Still they have given me a script to write with the understanding that it will take me a...
5
by: ozzii | last post by:
Hi, I have a asp email script which uses cdosys to send emails with attahcments by iterating through a record set of 500 email addresses. However the script simply times out. I know you can...
20
by: Pete Marsh | last post by:
Wondering if anyone can see an error with this script. I get a server configuration error. THat could mean a module is not being loaded, but maybe there's a syntax error here, can anyone spot it?...
1
by: harekrishna | last post by:
Hiii Can some please check my email script. I have gone through it several times but my mails won't come!! here is the script thank you- . .
7
by: SM | last post by:
Hello, I have a index.php template (2 columns). The right columns contains a bunch of links (interviews, poems, etc...) The left columns contains the actual article. So if I click on a link on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.