Jump to content

Help with email code please


geroid

Recommended Posts

Hi

I'm trying to write my first email script. Well actually I've downloaded the following one to see how it all works. The form page looks like this:

 

<form name="contact" method="post" action="contactprocess.php">
    <strong>Name:</strong><br/>
    <input type="text" name="ename" size="30"><br/>
    <strong>Email:</strong><br/>

    <input type="text" name="eemail" size="30"><br/>
    <strong>Subject:</strong><br/>
    <input type="text" name="esubject" size="30"><br/>
    <strong>Message:</strong><br/>

    <textarea name="emessage" cols="30" rows="10"></textarea><br/>
    <input type="submit" name="esubmit" value="Send Mail" style="cursor:pointer">
    <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
  </form>

 

The processing page looks like this:

 

<?php

  //Some variables
  $mymail = "geroido@hotmail.com";
  $ename = $_POST['ename'];
  $eemail = $_POST['eemail'];
  $esubject = $_POST['esubject'];
  $emessage = $_POST['emessage'];
  $eip = $_POST['eip'];


  //Function to check email address
  function checkemail($eemail) { 
    if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail))  { 
      return true; 
    } 
    else { 
      return false; 
    } 
  }

  //Mail Processing
  if ($_POST['esubmit']) {
    //Check for blank fields
    if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") {
      echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>";
    }
    //Check to see if the email address is valid
    else if (checkemail($eemail) == false) { 
       echo "<p>It appears that you enter an invalid email address.<br/> Please check your email again.</p>";
    }
    //Send the email if there's no error
    else {
      $body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip";
      mail($mymail,$esubject,$body,"From: $eemail\n");
      echo "<p>Thank you for your email $ename!</p>";
    }
  }

?>

 

Now I'm just testing this on my xammp server at home and I get the following error message:

 

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\glorcar\contactprocess.php on line 60

 

Thank you for your email!.

 

Will the mail function work from home (I'm' developing this site to be uploaded to a hosting company. How can I test it to see if it works from home?

 

Thanks

Link to comment
Share on other sites

It can be used from your localhost server, but it has the restriction as on a remote server. The server must be configured correctly for it to work. In order to send e-mails you require an SMTP server, many paid hosts provide one for you and automatically configure your server. For use on your localhost you will need access to an SMTP server and you will have to setup your system to use it. As it happens the error message actually informed you how to do this.

 

Depending on what ISP you have they often provide an SMTP server that you can use. For example I live in the UK and my ISP is VirginMedia (previously NTLWorld) and I know that my SMTP server is smtp.ntlworld.com. The one restriction is that the mail() function in PHP does not support authentication so if your SMTP server requires authenticaion, you will not be able to use it.

 

By default on a Xampp install the php.ini file will be located at c:\xampp\php\php.ini, just locate the settings listed in the error message and change them. To confirm that the location I stated above is the php.ini file in use you can just create a new php file with <?php phpinfo(); ?> written in it and run it, one of the bits of information given is the active php.ini file.

 

Hopefully that helps, if need any clarification let me know.

Link to comment
Share on other sites

Thanks cags

i'm''néw to this and am a little confused. I'vé opened my php.ini file and found the following lines:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

 

I'm running the mercury mail server on xampp. I really have no idea how to set this up or how to configure it. Do you think the email code will work? what changes do I need to make? I had forgotten to turn mercury on last attempt but have done so now. The error message now is:

 

SMTP server response: 553 We do not relay non-local mail, sorry.

Link to comment
Share on other sites

To be honest with you I couldn't say. I've never used Mercury (or any local mail server) as I've always relied on my ISP's SMTP server. The section you have found in the php.ini file is certainly the right part. It's just a matter of knowing what to write in place of 'localhost'. I suspect Mercury should be able to provide you with that information.

 

Since your new to this I should also point out, in order to reflect any changes made to php.ini, you will have to restart Apache.

Link to comment
Share on other sites

I've tried sending mail from xampp before and had zero luck - it's a nightmare.

 

Basically in order to actually send email you need a mail server. Mercury allegedly does this but I couldn't get it to work. I also tried squirrel mail but that didn't work either.

 

Suffice to say, if you're eventually running the scripts on a unix server, just trust that mail() will work unless the server admin is highly retarded.

 

What I usually do is check if the script is running on a test or production server by use of a DEBUG constant, then either print/save the emails to the screen or a file in I'm on the test server, or call mail() if it's running on the live production server.

Link to comment
Share on other sites

Thanks

Guess I'll just have to wait until the script is on the host and iron out any problem then with the support people. That's ok. Could I just ask you to have a quick look at the script and tell me if you think it looks ok. I just need a simple mail script for the users to email the website people .

 

<b><font size=2 color=green> Please complete the email form below and select '</font><font size=2 color=red> Send Mail</font><font size=2 color=green> '. All fields are required.</font><BR><BR>
  <form name="contact" method="post" action="contactprocess.php">
    <strong>Name:</strong><br/>
    <input type="text" name="ename" size="30"><br/>
    <strong>Email:</strong><br/>

    <input type="text" name="eemail" size="30"><br/>
    <strong>Subject:</strong><br/>
    <input type="text" name="esubject" size="30"><br/>
    <strong>Message:</strong><br/>

    <textarea name="emessage" cols="30" rows="10"></textarea><br/><br>
    <input type="submit" name="esubmit" id="submitinput" class="highlightit" value="Send Mail" style="cursor:pointer">
    <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
  </form>

 

Processing page:

 


<?php

  //Some variables
  $mymail = "geroido@hotmail.com"; //Not the real thing here, just an example
  $ename = $_POST['ename'];
  $eemail = $_POST['eemail'];
  $esubject = $_POST['esubject'];
  $emessage = $_POST['emessage'];
  $eip = $_POST['eip'];


  //Function to check email address
  function checkemail($eemail) { 
    if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail))  { 
      return true; 
    } 
    else { 
      return false; 
    } 
  }

  //Mail Processing
  if ($_POST['esubmit']) {
    //Check for blank fields
    if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") {
    ?><b><font size=2 color=green> Please complete the email form below and select '</font><font size=2 color=red> Send Mail</font><font size=2 color=green> '. All fields are required.</font><BR><BR><?

      echo "<p><Font color=red>It appears that you have left a <em>blank field</em>.<br/> Please make sure you fill everything in.</font></p>";
    ?>   <form name="contact" method="post" action="contactprocess.php">
    <strong>Name:</strong><br/>
    <input type="text" name="ename" size="30" value="<?echo $ename?>"><br/>
    <strong>Email:</strong><br/>

    <input type="text" name="eemail" size="30" value="<?echo $eemail ?>"><br/>
    <strong>Subject:</strong><br/>
    <input type="text" name="esubject" size="30" value="<?echo $esubject?>"><br/>
    <strong>Message:</strong><br/>

    <textarea name="emessage" cols="30" rows="10"><?echo $emessage?></textarea><br/><br>
    <input type="submit" name="esubmit" id="submitinput" class="highlightit" value="Send Mail" style="cursor:pointer">
    <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
  </form><?
    }
    //Check to see if the email address is valid
    else if (checkemail($eemail) == false) { 
        ?><b><font size=2 color=green> Please complete the email form below and select '</font><font size=2 color=red> Send Mail</font><font size=2 color=green> '. All fields are required.</font><BR><BR><?

       echo "<p><font color=red>It appears that you have entered an <em>invalid</em> email address.<br/> Please check your email again.</font></p>";
        ?>   <form name="contact" method="post" action="contactprocess.php">
    <strong>Name:</strong><br/>
    <input type="text" name="ename" size="30" value="<?echo $ename?>"><br/>
    <strong>Email:</strong><br/>

    <input type="text" name="eemail" size="30" value=""><br/>
    <strong>Subject:</strong><br/>
    <input type="text" name="esubject" size="30" value="<?echo $esubject?>"><br/>
    <strong>Message:</strong><br/>

    <textarea name="emessage" cols="30" rows="10" ><?echo $emessage?></textarea><br/><br>
    <input type="submit" name="esubmit" id="submitinput" class="highlightit" value="Send Mail" style="cursor:pointer">
    <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
  </form><?
    }
    //Send the email if there's no error
    else {
      $body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip";
      mail($mymail,$esubject,$body,"From: $eemail\n");
      echo "<p>Thank you for your email $ename ($eemail)!</p>";
    }
  }

?>

Link to comment
Share on other sites

Looks ok to me. Just a couple of notes....

 

- Perhaps not the easiest structure in terms of handling the HTML when errors occur, but thats just my opinion.

- Looks like in a few places your using short tags (<? ?>), this is a bad idea, many hosts don't support it by default

- if ($_POST['esubmit']) could theoretically cause a "Notice: Undefined index...". But assuming the second block of code is a seperate page to the first, this will only ever happen if somebody navigates directly to it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.