Hi All,

Having some troubles getting my Mail Headers to work. I can get most of them to work so that the email displays as HTML using the Mime types, but as soon as I add the "from:" email address part, the emails fail to send.

I've tried the script without using the headers, and it sends, and then I've tried the script with one header at a time, and it always fails when I add the From: part, but only if I add the Variable.

I've looked up the function on PHP.net and I'm quite familiar with the mail() function anyway, I just cant understand for the life of me why it wont send using a variable for the From: section.

Now, I'm pretty sure it's to do with the variables... because if I add the section of code straight from php.net the mail will send with the from: headers filled in correctly, using this:

$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";

Here's the code I'm using, which is basic - but in theory should work...

form.html

<form action="send.php" method="post">
<input type="text" name="email" value="YourMail"><br>
<input type="text" name="subject" value="Subject"><br>
<textarea name="message"></textarea><br>
<input type="submit" name="submit" value="Submit"><BR>
</form>

send.php

<?php

$ownermail = 'phil@phildiggle.co.uk';
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = nl2br($_POST['message']);

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";


mail($ownermail,$subject,$message,$headers);

?>

If you take the last $headers variable out, the script works. Add in the last headers variable and the script fails. It's probably something simple that I'm over looking, but I can't understand why it wont work using a variable for the email address...

Cheers for looking!

    Yeah, like I said in the original post, adding that example the message sends with the proper From: header set, but as soon as I add the variable for the mail address the mail never sends.

      try and print the $headers

      echo $headers;

      compare your version and the example from the manual.

        headers are printed as such:

        MIME-Version: 1.0 Content-type: text/html; charset=iso-8859-1 From: phil.diggle@gmail.com

        To me, that's how it should come out - the variable for the email address is passed through correctly and it's put in correctly for the From part... yet the email doesn't actually send with this header put in place.

        I've elimated the line for the From: header and the message sends. Put the line back in, and problem starts again

          try this.

          ini_set('Sendmail_from',$from);
          $headers .= 'From: $from <$from>' . "\r\n"; 

          before you use a variable, make sure it has a value ( isset() )

          try to download a phpmailer class, and send mails with it.

            Thanks for your help so far.

            I actually forgot about the ini_set function, however I've just put that in my script and it's still not sending the emails.

            I've downloaded about 10 mailer scripts now, and even though people say they work etc, I can't actually get any mails to send, other than when I take out the From: header sections. It's really bugging me now lol.

            PHP info can be found here: www.phildiggle.co.uk/ghmotors/contact/info.php

              if the from email header is not an address at the host sending the email, and your on something like a shared host, that could be blocked

                @: Thanks for that, I'll have a look at that in a minute.

                @: If that was the case, wouldn't the example from PHP.net fail too? Which I've just tried again and still works, yet as soon as I put a variable in, it fails.

                  phildiggle;10935908 wrote:

                  @: If that was the case, wouldn't the example from PHP.net fail too? Which I've just tried again and still works, yet as soon as I put a variable in, it fails.

                  that would depend on the host.

                    @: Thanks, but I can't get the form to load for some reason - i'll do some investigating soon.

                    @:I see where you're coming from....

                    Ok, so this is where I've got to in bug solving... If I put the From: header manually as per php.net, it sends fine. If I change the header to put the email address in manually, it'll send if I don't put in hotmail or gmail as the address domain, but if I put in say an actual domain (bellbrokers.co.uk is one example) then the message sends fine.

                    So - do you think its the host that's blocking the use of say hotmail, gmail, yahoo address to send mail through forms? And if so, is there a way around that?

                      As thought, I cannot get emails to send on the form if they contain well known domains such as:

                      aol.com
                      hotmail.com ... or hotmail.co.uk
                      gmail.com
                      yahoo.com

                      however, if I add an unknown domain after the @ symbol, the email sends fine. So it appears that there is some form of block or something stopping the mailer sending emails if it contains any of the above. Would there be someway of correcting this through ini_set(); or .htaccess?

                      EDIT: I know there are problems with PHP Mail function going out to Hotmail address etc, however, that's not the problem here. It's a problem receiving from hotmail address as opposed to sending to a hotmail address.

                        how is the script hosted?

                          its hosted on a website... with my usual hosts. Linux hosting if I remember rightly.

                          Never really had a problem with anything else on the hosting

                            this looks like a host specific issue, your first port of call is their support system.

                              Dagon,
                              I've contacted my host and they state there are no blocks on the server to stop the script from sending emails containing hotmail address. They simply tell me to add the '-f' declaration to the mail function.

                              So, I've followed their example to the word - and I still get the same problem! Which is really starting to annoy me now.

                              Send.php now looks like this:

                              <?php
                              $webMaster = "enquiry@phildiggle.co.uk";
                              
                              $emailField = $_POST['email'];
                              $subjectField = $_POST['subject'];
                              $SendEmail ="administrator@phildiggle.co.uk";
                              
                              $message = nl2br($_POST['message']);
                              
                              $headers = "From: $emailField\r\n";
                              $headers .= "Content-type: text/html\r\n";
                              
                              mail($webMaster, $subjectField, $message, $headers, '-f'.$SendEmail);
                              
                              print $headers;
                              ?>

                              Grrrrrrr

                                Write a Reply...