Website not sending email when it should

Discussion in 'Web Design and Development' started by sugarbabee, May 14, 2008.

  1. sugarbabee macrumors newbie

    Joined:
    May 12, 2008
    #1
    On the site I am working on when someone submits a form, the information from the form should be emailed to one of two people depending on the topic (it's a feedback form). Everything about the form works...it just isn't sending the emails like it's supposed to.

    After inputting the user previews their info to make sure it's correct, then if it is they submit at which point it should be sent through email and to a database:

    So the send() method is called...im new to php so it looks like it is anyway lol any reason for the mail not to send? thanks for any help :)
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    First, when posting PHP code you should use the PHP button (the one labeled PHP) so it can color code it, which makes it look nicer and adds a scroll bar around it, and overall makes it easier to debug. You can edit your post and add the text [ PHP ] and [ /PHP ] around the PHP (without the spaces) to fix it.

    PHP:
    } elseif ($_SESSION['feedback']) { # Coming back from preview
    Comment are generally done with // not #. That might be causing issues.

    I don't see where you check if the 'action' is 'send' which would call your send function and email the information.

    You need something like,
    PHP:
    if ($_POST['action'] == 'send'send();
    somewhere in your code.
     
  3. sugarbabee thread starter macrumors newbie

    Joined:
    May 12, 2008
    #3
    Where would i put a statement like that? would it be in the preview section or somewhere else?
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    In place of,
    PHP:
    $cmd '$C->' $_REQUEST['action'] . '();';
    eval(
    $cmd); 
    you can do,
    PHP:
    $action $_POST['action'];
    switch (
    $action) {
      case 
    'form'$C->form(); break;
      case 
    'send'$C->send(); break;
      case 
    'success'$C->success(); break;
      default: 
    /* unrecognized action type sent, take appropriate action */
        
    break;
    }
    I believe that'll work. Note: $_POST and $_REQUEST are accessing the same variables. I used post simply because that's what I'm use to.
     
  5. sugarbabee thread starter macrumors newbie

    Joined:
    May 12, 2008
    #5
    I replaced it so now my code is:

    PHP:
    $C = new Controller;

    if(!
    array_key_exists('action'$_REQUEST)) {
        
    $_REQUEST['action'] = 'form';
    }


    $action $_REQUEST['action'];
    switch (
    $action) {
      case 
    'form'$C->form(); break;
      case 
    'send'$C->send(); break;
      case 
    'preview'$C->preview(); break;
      case 
    'success'$C->success(); break;
      
    }  
    but it seems to be doing the same thing :( I don't understand why this isn't working..
     
  6. sugarbabee thread starter macrumors newbie

    Joined:
    May 12, 2008
    #6
    is there some way I could check to see what my browser does when I click submit? I know stuff goes into our database but I'm not sure if it's even trying to send mail...maybe it can't send an email? lol i dont know
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Are any errors showing up in your PHP error log? You can also add some print statements to the code so that you can see which areas the code is making it to and even output some variables to see what they are set to at those points. This'll debug what's going on.

    You should also check if there are any errors when you do your mail call.
    PHP:
    $mail_success mail(...);
    if (!
    $mail_success) {
      
    // There was a problem sending the mail
      
    print '<p>There was an error: '$mail_success->getMessage() .'</p>';
    }
    I'm not sure if the getMessage() will work here. It's what I use on my mail that I use with PEAR, but won't necessarily work here. You'll at least know if there was a mail problem though.
     
  8. sugarbabee thread starter macrumors newbie

    Joined:
    May 12, 2008
    #8
    Oh i forgot I turned these off...it is telling me:

    Notice: Undefined index: http_referer in /fsys2/www/html/cpadev/_feedback/index.php on line 40

    Notice: Undefined index: HTTP_REFERER in /fsys2/www/html/cpadev/_feedback/index.php on line 41

    Notice: Undefined index: action in /fsys2/www/html/cpadev/_feedback/index.php on line 46

    Line 46: if ($_POST['action']) { in the form() function

    it must be the acrtion one screwing up the emailing...how do i define it then? I don't think it's undefined or nothing would really work...since the form(), preview() and success() work...and info is being saved to the database.
     
  9. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #9
    I access the referrer with $_SERVER['HTTP_REFERER']. That should be all you need for that first part.

    For the action check do,
    PHP:
    if (isset($_POST['action'])) {
     
  10. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #10
    Consider this an "alternative" reply, not intended to be a followup to any of the excellent advice given above by others.

    May I suggest using an open source PHP class (object oriented) which is intended to make life easier for you as a developer by handling common tasks like this? I've file attached a zip containing 3 LGPL class files and one test script. It handles sendmail, SMTP, file attachments, MIME and HTML, bcc, cc and so on. It also has a basic external language file for customizing certain output, and a respectable error handler. Good basic software tool for most mailing situations you can port easily. Please download then modify/run test.php which demos basic functionality including importing a CSS file for fancy HTML and attachment handling. As you're new to PHP you might find examining these class files a great way to learn PHP and one day you might start writing your own classes, even if you stick to your own code.

    If the attached class doesn't work for you then plan B might be to visit phpclasses.org, a free repository for developers which is excellent:

    Numerous examples found here (all free)

    I can't suggest one from that list because everyone has different needs, but that site (phpclasses.org) is 100% free and a great source for things like this.

    I posted this because when people write scripts to handle email tasks, it's a common thing so I immediately thought trying an OOP class might be a worthwhile suggestion. Not intending to step on any toes here.

    -jim
     

    Attached Files:

  11. sugarbabee thread starter macrumors newbie

    Joined:
    May 12, 2008
    #11
    Okay so I'm still getting the second notice above, and I did that mail test thing and it is working because it did everything it was supposed to...except send the email lol, no error message
     
  12. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #12
    PHP's mail command hands off to the server's MTA (i.e. sendmail, exim) and if the handoff is okay, no error. What happens after depends on the MTA returning an error code or not. Assuming your script is correct in all regards, it could be your server's sendmail isn't working properly, filters in effect, and maybe all outbound mail is being sent to the mail queue and stuck due to a localhost or DNS setup issue or whatever. Things your network or server admin would check into to ensure scripts can E-Mail outbound if that person is not you. Betcha the server has the errors logged, or wherever the MTA writes its event log!

    Test your MTA locally, i.e. not using PHP by sending email via command line or see if is the MTA requires SUEXEC since PHP normally runs as user "nobody".

    I also might suggest trying the SMTP method using an offsite server and your login credentials if the host allows it.

    As to the $_SERVER['HTTP_REFERER'] error:

    If there is no referrer in existence then this variable will not be defined so in PHP you always test if a variable has been set using "isset", i.e.

    PHP:
    if(isset($_SERVER['HTTP_REFERER'])) { ...whatever ... }
    -jim
     
  13. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #13
    Since it's only a notice, and not an error, I suggest ignoring it for the moment. It's possible that the mail function won't work here.

    I read about the mail function and it says even if it returns true, that doesn't mean it'll be sent where it's suppose to go. There could be a SMTP blocking issue. I have to use my ISP's SMTP server when sending emails out from my site and use a specific port. There could be a similar situation for your server. I also don't really know if this mail function supports SMTP.

    I use Mail::pEAR. Here's a small tutorial on how to use it. It has the advantage of telling you if the mail was really successful or not. This'll depend on if the server has the PEAR Mail class installed as well. And here's the PEAR::Mail site.
     
  14. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #14
    @ sugarbabee - the class I uploaded supports SMTP. Enjoy!

    -jim
     

Share This Page