PHP Email Script - Need advice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbrewerton
    New Member
    • Nov 2009
    • 115

    PHP Email Script - Need advice

    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:

    Code:
    if ($to=="sales@mydomain.com")
    $subject="RFQ"
    mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site RFQ request.")
    elseif ($to==webmaster@mydomain.com)
    $subject="Website Related";
    mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site website request.")
    elseif ($to==support@mydomain.com)
    mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site support request.")
    $subject="Account support request";
    else
    $subject="Contact Request";
    mail("1112223333@messaging.sprintpcs.com","","noreply@mydomain.com","You have received a site contact request.")
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Your if structure is completely off. You're missing a bunch of brackets and semicolons. The correct if structure is as follows
    Code:
    if (expression) {
       statement;
    } elseif (expression) {
       statement;
    } else {
       statement;
    }

    Comment

    • dbrewerton
      New Member
      • Nov 2009
      • 115

      #3
      I actually re-wrote this using case statements instead, way faster than if elseif else loop.

      Comment

      • dbrewerton
        New Member
        • Nov 2009
        • 115

        #4
        And here is the re-written code block
        Code:
        $array = array('1'=>'info@mydomain.com',
                        '2'=>'support@mydomain.com',
                        '3'=>'sales@mydomain.com',
                        '4'=>'webmaster@mydomain.com');
                        
        if (isset($_POST['Dept']))
        {
          $to = $_POST['Dept'];
        } else {
        $to = 'default';
        
        switch ($to) {
          case 1:
          $subject = "General Info Request";
          $mailto = $array['1'];
          break;
        
          case 2:
          $subject = "Account Support Request";
          $mailto = $array['2'];
          break;
        
          case 3:
          $subject = "Request For Quote from $name - $from";
          $mailto = $array['3'];
          mail("1112223333@vtext.com","","RFQ request from $name - $from waiting in your email.");
          break;
        
          case 4:
          $subject = "Website assistance request from $name - $from";
          $mailto = $array['4'];
          break;
          
          default:
          $subject = "No department chosen - to catch-all.";
          $mailto = "webmaster@mydomain.com";
          break
        }

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Another option would be to store all the information in an array and then you can drop the case statements altogether.

          Comment

          • dbrewerton
            New Member
            • Nov 2009
            • 115

            #6
            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.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

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

              If you get around to it, basically you would do something like this
              Code:
              $arr = array(
                 'John Smith' => array(
                    'Subject' => 'Hi',
                    'EMail' => 'John.Smith@domain.com'
                 )
                 'Jane Doe' => array(
                    'Subject' => 'Hello',
                    'EMail' => 'Jane.Doe@gmail.com'
                 )
              );
              
              $arrUser = $arr[$user];
              sendMail($arrUser['Subject'], @arrUser['EMail']);

              Comment

              • dbrewerton
                New Member
                • Nov 2009
                • 115

                #8
                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?
                Code:
                <?php
                $dept=$_REQUEST['Dept'];
                $from=$_REQUEST['Email'];
                $name=$_REQUEST['Name'];
                $headers="From: noreply@mydomain.com";
                $array = array('1'=>'info@mydomain.com','2'=>'support@mydomain.com','3'=>'sales@mydomain.com','4'=>'webmaster@mydomain.com');
                if (isset($_POST['Dept']))
                {
                  $to = $_POST['Dept'];
                }
                else
                {
                $to = 'default';
                }
                switch ($to)
                {
                  case 1:
                  $subject = "General Info Request";
                  $mailto = $array['1'];
                  break;
                  case 2:
                  $subject = "Account Support Request";
                  $mailto = $array['2'];
                  break;
                  case 3:
                  $subject = "Request For Quote from $name - $from";
                  $mailto = $array['3'];
                  mail("1112223333@mytext.com","","RFQ request from $name - $from waiting in your email.");
                  break;
                  case 4:
                  $subject = "Website assistance request from $name - $from";
                  $mailto = $array['4'];
                  break;
                  default:
                  $subject = "No department chosen - to catch-all.";
                  $mailto = "webmaster@mydomain.com";
                  break;
                }
                $fields=array();
                $fields{"Name"}="Name";
                $fields{"Company"}="Company";
                $fields{"Email"}="Email";
                $fields{"Phone"}="Phone";
                $fields{"list"}="Mailing List";
                $fields{"Message"}="Message";
                $body="We have received the following information:\n\n"; 
                foreach($fields as $a => $b)
                {
                $body=sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
                }
                $headers2="From: noreply@mydomain.com";
                $subject2="Thank you for contacting us";
                $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";
                if($from=='')
                	{
                print "You have not entered an email, please go back and try again";
                	}
                else
                	{
                	if($name=='')
                	{
                		print "You have not entered a name, please go back and try again";
                	}
                	else
                	{
                		$send=mail($mailto, $subject, $body, $headers);
                		$send2=mail($from, $subject2, $autoreply, $headers2);
                		if($send)
                		{
                			header("Location:http://www.mydomain.com/prototype/thankyou.html");
                		}
                		else
                		{
                			print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
                		}
                	}
                }
                ?>

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  It doesn't even send to default?

                  Comment

                  • dbrewerton
                    New Member
                    • Nov 2009
                    • 115

                    #10
                    Nope, I just tried and no dice.

                    Comment

                    • dbrewerton
                      New Member
                      • Nov 2009
                      • 115

                      #11
                      Actually, sales & support work. The info and webmaster one don't.

                      Comment

                      • dbrewerton
                        New Member
                        • Nov 2009
                        • 115

                        #12
                        SO it would appear that case 1 & 4 do not work.

                        Comment

                        • aelisenko
                          New Member
                          • Oct 2011
                          • 7

                          #13
                          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 " = ".



                          Code:
                          <?php
                          $dept = $_REQUEST['Dept'];
                          $from = $_REQUEST['Email'];
                          $name = $_REQUEST['Name'];
                          
                          $headers = "From: noreply@mydomain.com";
                          
                          $mail_settings = array(
                          					   '1' => array(
                          								'to' => 'info@mydomain.com',
                          								'subject' => 'General Info Request'
                          								),
                          					   '2' => array(
                          								'to' => 'support@mydomain.com',
                          								'subject' => 'Account Support Request'
                          								),
                          					   '3' => array(
                          								'to' => 'sales@mydomain.com',
                          								'subject' => "Request For Quote from $name - $from"
                          								),
                          					   '4' => array(
                          								'to' => 'webmaster@mydomain.com',
                          								'subject' => "Website assistance request from $name - $from"
                          								),
                          						'default' => array(
                          								'to' => 'webmaster@mydomain.com',
                          								'subject' => 'No department chosen - to catch-all.'								
                          								)								
                          					   );
                               
                          	 
                          $to = (isset($_POST['Dept'])) ? $_POST['Dept'] : 'default';
                              
                          $subject = $mail_settings[$to]['subject'];
                          $mailto  = $mail_settings[$to]['to'];
                               
                          $fields = array(); //#1
                          $fields["Name"]    = "Name";
                          $fields["Company"] = "Company";
                          $fields["Email"]   = "Email";
                          $fields["Phone"]   = "Phone";
                          $fields["list"]    = "Mailing List";
                          $fields["Message"] = "Message";
                          
                          $body = "We have received the following information:\n\n"; 
                          
                          foreach($fields as $a => $b){
                          	
                          	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);  //#2
                          
                          }
                          
                          $headers2  = "From: noreply@mydomain.com";
                          $subject2  = "Thank you for contacting us";
                          $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";
                          
                          if($from == ''){
                          
                          	print "You have not entered an email, please go back and try again";
                          
                          }else{
                          	
                              if($name == ''){
                          		
                                  print "You have not entered a name, please go back and try again";
                              
                          	}else{
                          		
                                  $send  = mail($mailto, $subject, $body, $headers);
                                  $send2 = mail($from, $subject2, $autoreply, $headers2);
                                  
                          		if($send){
                          			
                                      header("Location:http://www.mydomain.com/prototype/thankyou.html");
                                  
                          		}else{
                          			
                                      print "We encountered an error sending your mail, please notify webmaster@mydomain.com";
                                  
                          		}
                              }
                          }
                          ?>

                          Comment

                          • dbrewerton
                            New Member
                            • Nov 2009
                            • 115

                            #14
                            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 :)

                            Comment

                            Working...