Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: mail() Problem

  1. #1
    Senior Coder chilipie's Avatar
    Join Date
    Jul 2004
    Location
    Shrewsbury, UK Age: 16
    Posts
    1,117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mail() Problem

    Not working for some reason. It's probably some really stupid mistake - I'm pretty crap at PHP .
    PHP Code:
    <?php

    if($submit) {

    if (
    $Name && $Email && $Subject && $Message) {

    mail("ollie.craig@virgin.net""$Subject""$Message""From: $Name - $Email") or die("Sorry, there has been an error.");

    header("refresh: 0; url=contactdone.php");

    } else {

    echo 
    "You must fill in all of the fields.";

    }

    }

    ?>
    Thanks,
    Ol.
    Last edited by chilipie; 11-22-2004 at 07:32 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When you say "not working", what do you mean exactly?

    Is nothing happening? Are you getting a blank page?

    Or does it print "You must fill in all of the fields.", or "Sorry, there has been an error."?

    Aside from the email being sent, what else is supposed to happen? Maybe you should input some more debugging code (more echoes, etc..). Is something happening that isn't supposed to be happening?

    Perhaps some of those variables you're testing for don't exist? IE. $Name should actually be $_GET['Name'] or $_POST['Name'] or something...

    Regardless, your post was quite vague and it's hard to troubleshoot what the problem is as it could be anything. Please fill in some blanks and hopefully we can help you resolve this issue.

    Good luck,
    Sadiq.

  • #3
    Senior Coder chilipie's Avatar
    Join Date
    Jul 2004
    Location
    Shrewsbury, UK Age: 16
    Posts
    1,117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I should have been more specific.

    Basically, when I try to submit the form (with or without data in the input fields) I just end up at a blank page. For in action: http://redblizzard.astahost.com/contact.php

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I think it's got to do with accessing your variables ($Name versus $_GET['Name']).

    Try this code instead:
    PHP Code:
    <?php 

     
    if(isset($_GET['submit'])) { 
      echo 
    'Submit is set!<br/>';
      if (!empty(
    $_GET['Name']) && !empty($_GET['Email']) && !empty($_GET['Subject']) && !empty($_GET['Message'])) {
       echo 
    'Sending email!</br>'
       
    mail("ollie.craig@virgin.net""$_GET['Subject']""$_GET['Message']""From: $_GET['Name'] - $_GET['Email']") or die("Sorry, there has been an error."); 

       
    header("refresh: 0; url=contactdone.php"); 
      } 
      else { 
       echo 
    "You must fill in all of the fields."
      } 
     } 
     else {
      echo 
    'Submit not set...<br/>';
     }

    ?>
    If that seems to work after your various testing you can remove the unnecessary echoes as you see fit.

    Another area where you may or may not run into problems is in your php.ini file: you need to setup your SMTP server for the mail() function. If it's not set to a valid SMTP server, your mail() function won't send emails anywhere and may error out...

    Edit:
    You may or may not have noticed this (or known this), but I accessed your form elements through the $_GET array as the METHOD attribute of your form held GET as the value. Had it held POST, I would've used the $_POST array to access your form elements.


    Hope that helps,
    Sadiq.
    Last edited by sad69; 11-22-2004 at 08:25 PM. Reason: get/post edumacation..

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    also (read Carl_McDades post in this forum) use location header not refresh...

    header("location:contactdone.php");
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Senior Coder chilipie's Avatar
    Join Date
    Jul 2004
    Location
    Shrewsbury, UK Age: 16
    Posts
    1,117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sad69
    Another area where you may or may not run into problems is in your php.ini file: you need to setup your SMTP server for the mail() function. If it's not set to a valid SMTP server, your mail() function won't send emails anywhere and may error out...
    I don't have access to the sever, so that might be a problem. However, I've used PHPFormGenerator, and the mail() function in the form worked then.

    I got a parse error this time...
    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/redblizz/public_html/contactprocess.php on line 7
    I'll try with POST instead of GET, and see if that works.

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure if this is the problem (I never keep variables inside of double-quotes, I usually close the string and concatenate...).

    One of the following 2 line 7's might work:
    PHP Code:
    mail("ollie.craig@virgin.net""{$_GET['Subject']}""{$_GET['Message']}""From: {$_GET['Name']} - {$_GET['Email']}") or die("Sorry, there has been an error."); 
    or:
    PHP Code:
    mail("ollie.craig@virgin.net"$_GET['Subject'], $_GET['Message'], "From: ".$_GET['Name']." - ".$_GET['Email']) or die("Sorry, there has been an error."); 
    Let me know if that works.

    Sadiq.

  • #8
    Senior Coder chilipie's Avatar
    Join Date
    Jul 2004
    Location
    Shrewsbury, UK Age: 16
    Posts
    1,117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope :shakes head:
    I've been looking at the documentation about the mail() function on PHP.net, but can't seem to find anything wrong...

  • #9
    New Coder
    Join Date
    Nov 2004
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is my version of the code. It first checks to see if any of the variables you are sending are empty if not then it sends you the email. I have tested this script of mine and it works 100% without problems! I did notice that someone could easily flood you with emails by going to that url six or seven hundred times. I suggest you add a cookie that limits how many emails they could send per hour and maybe add some other protection.

    Code:
    <?php
    
    error_reporting(0);
    
    if(!empty($_GET['Name']) and !empty($_GET['Email']) and !empty($_GET['Subject']) and !empty($_GET['Message']))
    {
    
    mail("ollie.craig@virgin.net",$_GET['Subject'],$_GET['Message'],"From:<".$_GET['Name'].">".$_GET['Email']) or die("Mail() Failed.");
    
    header("refresh: 0; url=contactdone.php");
    }
    else
    {
    die("You left a part of the form blank. Please hit your browsers back button and verify you have all parts of the form filled out.");
    }
    
    ?>
    Nikita
    Контакт: Nikita.05@gmail.com

  • #10
    Senior Coder chilipie's Avatar
    Join Date
    Jul 2004
    Location
    Shrewsbury, UK Age: 16
    Posts
    1,117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Nikita. Works fine .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •