How to set up mail?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

How to set up mail?

Postby jcafaro10 » 17. May 2008 17:19

I'm trying to have email get sent to me when a user of my website clicks the form button. I'm previewing it on my computer using xampp and its not working.

Here is the html/php code I'm using:
Code: Select all
 <form action="" method="post" name="email" id="email">
    <p>
      <label for="emailAdd" class="question">Email:</label>
      <input name="emailAdd" type="text" id="emailAdd" size="30" />
    </p>
    <p>
      <label for="comment" class="question">Question:</label>
      <textarea name="comment" id="comment" cols="30" rows="5"></textarea>
    </p>
    <p>
       <input type="submit" name="submit" id="submit" value="Submit">
        <input type="hidden" name="submitted" value="1" />
    </p>
  </form>

  <?php
     if(isset($_POST['submitted']))
   {
      if(mail('myemail@mydomain.com','Contact Form Submission',$_POST['comment'],$_POST['emailAdd']))
      echo "Good";
      else echo "Bad";
   }
  ?>


How do I correctly set this up?

Here is the result:
Code: Select all
<b>Warning</b>:  mail() [<a href='function.mail'>function.mail</a>]: &quot;sendmail_from&quot; not set in php.ini or custom &quot;From:&quot; header missing in <b>C:\Program Files\xampp\htdocs\HTMLSite2\contact.php</b> on line <b>81</b><br />

Bad
jcafaro10
 
Posts: 15
Joined: 16. May 2008 05:43

From header is not correct.

Postby spekkionu » 18. May 2008 07:12

Your From header is not correct.

From the PHP Manual wrote: additional_headers (optional)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

Note: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.


Each header needs to start with which type of header it is (in this case From) and end with a CRLF (\r\n).

Your mail function should look more like this:
Code: Select all
mail('myemail@mydomain.com','Contact Form Submission',$_POST['comment'],'From: '.$_POST['emailAdd']."\r\n")


As a side note sticking user input directly into an email without filtering it first can be dangerous.

As a minimum you should make sure to remove any line breaks from $_POST['emailAdd'] so the field can't be used to manipulate the headers.

Something like:
Code: Select all
str_replace(array("\n","\r","\r\n"), "", $_POST['emailAdd'])


Other filtering such as htmlentities(), strip_tags(), addslashes(), or even custom filters are always a good idea when working with input.
spekkionu
 
Posts: 1
Joined: 18. May 2008 06:43

Re: From header is not correct.

Postby ifokkema » 19. May 2008 08:57

spekkionu wrote:As a side note sticking user input directly into an email without filtering it first can be dangerous.

As a minimum you should make sure to remove any line breaks from $_POST['emailAdd'] so the field can't be used to manipulate the headers.

Other filtering such as htmlentities(), strip_tags(), addslashes(), or even custom filters are always a good idea when working with input.

Thank you for pointing that out to him. I once had my email form misused as a spam sender because of using the submitter's email directly in the headers.
ifokkema
 
Posts: 56
Joined: 08. May 2008 13:05
Location: Leiden, Netherlands


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 132 guests