Problem with mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FenixDarkclaw
Forum Newbie
Posts: 2
Joined: Tue Jun 14, 2005 9:56 am

Problem with mail()

Post by FenixDarkclaw »

I have the follow code, which is suppose to take data from a form and send it in an email. Everything seems to work, the form submits, the page runs with no errors and even my little bit of error checking says the mail was sent. But the mail is never received. I've used formmail.pl and other prewritten script for this as well and I have the exact same problem. Any ideas?

Code: Select all

<?php
$message = '';
echo "POST/GET Variables: <br>";
foreach ($_POST as $key => $val)
if ($key != "recipient" && $key != "redirect" && $key != "print_config" && $key != "required" && $key != "Submit")
$message .=  "$val<br>";
$header = "From: ";
$header .= $email;
$header2 = "-f";
$header2 .= $email;
echo "$message";
$ok = mail($recipient, $subject, $message, $header, $header2);
if ($ok) {
echo "Mail succesfully sent to: ".$recipient;
} else {
echo "Error: Unable to send mail to: ".$recipient;
}
?>
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post by harrison »

Did you correctly configure you php.ini regarding SMTP server and port?
This section is located after a bunch of extensions config:

Code: Select all

&#1111;mail function]
; For Win32 only.
SMTP=localhost
smtp_port=25
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Where's your server? Is it a home server, a shared hosting server?
FenixDarkclaw
Forum Newbie
Posts: 2
Joined: Tue Jun 14, 2005 9:56 am

Post by FenixDarkclaw »

My php.ini looks like this:

Code: Select all

&#1111;mail function]
; For Win32 only.
SMTP=localhost
smtp_port=25
And my server is the server at my job (School server) which I have little knowledge of how it is set up unfortunatly.

This is quite the confusing development, and I apprieciate all the help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

At least two possibilities

1. SMTP isn't installed or running on localhost (it could be provided by an ISP).
2. The mail is being filtered as junk by the recipient

Try searching by the way. This has been asked umpteen times here before ;)
Last edited by Chris Corbyn on Wed Jun 15, 2005 7:20 am, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

one thing that catches my eye is

Code: Select all

foreach ($_POST as $key =&gt; $val)
if ($key != &quote;recipient&quote; &amp;&amp; $key != &quote;redirect&quote; &amp;&amp; $key != &quote;print_config&quote; &amp;&amp; $key != &quote;required&quote; &amp;&amp; $key != &quote;Submit&quote;)
$message .=  &quote;$val&lt;br&gt;&quote;;
the lack of brackets there but its still legal and all just wondering what exactly you want where cause they style gets confusing fast.

maybe just try doing somtin real simple like

Code: Select all

mail('myemail@server.com', 'did you get this email?', 'I HOPE YOU DID!');
and see if you end up getting it
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

that last field will likely set you back as it isn't a valid SMTP-header.
EDIT: 'I HOPE YOU DID!'
Post Reply