php e-mail script help

They have: 68 posts

Joined: Jun 2006

Hey guys,

Here's my problem...

I configured a php-email script on one server, but it no longer works on a new server of mine. I actually don't remember where i found the script, i think it was just off a university website or something. In any case, here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Arizona Belo - Ask the Managers</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#DD1032">
<body bgcolor="#DD1032">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0" width="56%" id="table17" cellspacing="0" cellpadding="0">
<tr>
<td width="435">
<?php
// SECTION 1
// if the variable "form_submitted" is true, it means that the "Submit" button has been pressed
if ($_POST['form_submitted']) { // if "true" process form
    
     // SECTION 2
     // get our form field values and put them in variables
    
$name = $_POST['name'];
    
$department = $_POST['department'];
    
$question = $_POST['question'];
    
    
// SECTION 3
     // Type your thank you message here.
    
$thank_you_message = "Thank you for submitting your question. You may now close this window.";
    
    
    
// SECTION 4
     // build the mail function
    
$to = "$email";
    
    
// Type the phrase you want to show up in the subject line
    
$subject = "Ask the Managers Question";
    
    
// Type the message you want to appear in the body of the email
    
$message = "Name: $name\n\nDepartment: $department\n\nQuestion: $question\n\n";
   
   
// SECTION 5
    // call mail function (will send the email)
    // the mail function returns true upon a successfull send so if the mail is sent you can say thanks
    // Change information after From: to match your name and email address
   
if (mail("*****","Ask the Managers Question","$message","From: Form Sender")) {
        echo
"$thank_you_message";
    } else {
        echo
"There was sort of problem that was beyond me.";
    }

// SECTION 6
} else { // else, show form
?>

<input type="hidden" name="form_submitted" value="true" />
<p style="margin-top: 0; margin-bottom: 0">
<img border="0" src="images/logos/askmanagerslogo.gif" width="435" height="100"></p>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<div align="center">
<table border="0" width="95%" id="table18" cellspacing="3" cellpadding="3">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0" align="left"><b>
<font color="#FFFFFF" face="Verdana" size="2">Name (Optional): </font></b> &nbsp;</p>
</td>
<td width="169">
<p align="center"><font face="Verdana">
<input input type="text" name="name" size="30" /></font></td>
</tr>
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0" align="left"><b>
<font color="#FFFFFF" face="Verdana" size="2">Department (Optional):</font></b> 
</p>
</td>
<td width="169"> 
<p align="center"> 
<font face="Verdana">
<input input type="text" name="department" size="30" /></font></td>
</tr>
</table>
</div>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<table border="0" width="99%" id="table19" cellspacing="0" cellpadding="0">
<tr>
<td>
<p align="center" style="margin-top: 0; margin-bottom: 0">
<font color="#FFFFFF" face="Verdana" size="2"><b>Question:</b></font></p>
<p align="center" style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<p align="center" style="margin-top: 0; margin-bottom: 0"><textarea rows="6" name="question" cols="33"></textarea></p>
<p align="center" style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<p align="center" style="margin-top: 0; margin-bottom: 0"><font face="Verdana">
<input input type="submit" name="submit" value="Ask!" style="font-family: Verdana; font-size: 12pt; color: #FFFFFF; font-weight: bold; font-style: italic; border: 2px outset #FFFFFF; background-color: #6F0205" /></font></p>
</td>
</tr>
</table>
<p align="center" style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<div align="center">
<table border="1" width="94%" id="table20" cellspacing="0" cellpadding="0" bordercolor="#6F0205" style="border: 1px solid #000000">
<tr>
<td bgcolor="#FFFFFF">
<table border="0" width="100%" id="table21" cellspacing="1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><b><font size="2" face="Verdana" color="#6F0205">Question 1:
Why isn't there more coffee in the break room?</font></b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#FFFFFF" size="2">&nbsp;We can't afford
it. Sorry.</font></td>
</tr>
</table>
</div>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<div align="center">
<table border="1" width="94%" id="table28" cellspacing="0" cellpadding="0" bordercolor="#6F0205" style="border: 1px solid #000000">
<tr>
<td bgcolor="#FFFFFF">
<table border="0" width="100%" id="table29" cellspacing="1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><b><font size="2" face="Verdana" color="#6F0205">Question 1:
Why isn't there more coffee in the break room?</font></b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#FFFFFF" size="2">&nbsp;We can't afford
it. Sorry.</font></td>
</tr>
</table>
</div>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<div align="center">
<table border="1" width="94%" id="table30" cellspacing="0" cellpadding="0" bordercolor="#6F0205" style="border: 1px solid #000000">
<tr>
<td bgcolor="#FFFFFF">
<table border="0" width="100%" id="table31" cellspacing="1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><b><font size="2" face="Verdana" color="#6F0205">Question 1:
Why isn't there more coffee in the break room?</font></b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#FFFFFF" size="2">&nbsp;We can't afford
it. Sorry.</font></td>
</tr>
</table>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</div>
<div align="center">
<table border="1" width="94%" id="table32" cellspacing="0" cellpadding="0" bordercolor="#6F0205" style="border: 1px solid #000000">
<tr>
<td bgcolor="#FFFFFF">
<table border="0" width="100%" id="table33" cellspacing="1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><b><font size="2" face="Verdana" color="#6F0205">Question 1:
Why isn't there more coffee in the break room?</font></b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#FFFFFF" size="2">&nbsp;We can't afford
it. Sorry.</font></td>
</tr>
</table>
</div>
</td>
<td>&nbsp;</td>
</tr>
</table>
<input type="hidden" name="form_submitted" value="true" />
</form>
<?php
}
?>

</body>
</html>
'

When i hit submit, it presents the "There was sort of problem that was beyond me." error (makes sense if you read the code).

Any guidance is appreciated. Thanks.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Take it from where you have an indication there is an error, and work your way back up the code to se what triggered it. Isolate each thing being done and test that one at a time. Being that this happened after moving to a new server, look for things that could cause the problems (some of these do not apply here, but just giving the list for general purposes):

- Differences between versions/configurations of PHP
- Differences in OS's or their versions/configurations
- Differences in versions/configurations of mySQL
- Differences in versions/configurations of Apache

The first thing that my eye spots above the error message is the call to the mail() function.

So start here, create a separate program to test that the basics of even using the mail() function.

<?php
$to
= '[email protected]';    // REPLACE THIS WITH YOUR E-MAIL ADDRESS
$subject = 'PHP mail Test';
$message = 'This is a test of PHP mail';
$headers = 'From: [email protected]' . \"\r\n\" .
    'X-Mailer: PHP/' . phpversion();

if (mail(
$to, $subject, $message, $headers))
  echo \"MAIL SENT\n\";
else
  echo \"MAIL FAILED\n\";
?>

This will let you know that the basic concept of sending mail from PHP works on the new server.

If this works, then I would look into your FROM field you are giving ("From: Form Sender"). You do not give an actual e-mail address there. Perhaps the new server demands that an actual e-mail address be in this field.

Also probably just something left over from where you got this script, you have $to = "$email"; which the variable $to isn't used elsewhere, and the $email isn't defined beforehand. Same with the line that starts $subject = "...

Along with general bad coding, you have the tag twice. There are some other areas where the coding could use improvement. If the form was submitted, the browser will get an opening tag and never get a closing one. I am assuming you are new and just learning coding, so keep an eye on things like that, and validate your pages (both the form, and the results versions).

Good luck!

-Greg

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

if (mail("*****","Ask the Managers Question","$message","From: Form Sender")) {
'
with '******' I am going to guess that's supposed to be your email address? And you should have more headers than just 'From: ', and 'Form Sender' should be an email address, even 'From: "Form Sender" '

if (mail("[email protected]","Ask the Managers Question","$message",'From: "Form Sender" <[email protected]>')) {
'

They have: 68 posts

Joined: Jun 2006

Quote: So start here, create a separate program to test that the basics of even using the mail() function.

I tried the basic mail function script you gave me, Greg... and got a failure.

What does that mean?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Ok, just ran a copy of it on my server to make sure that I didn't do a typo Wink (it works)

Now you have it down that it is not actually a problem with your code (although there are issues with it), so next check the server configuration. Is this your own server or a hosting company?

If it is a hosting company, contact them and let them know that you cannot send e-mail via php. I'd give them the location of the sample code you ran as well. This may help prevent an "easy reply". Always give them details of steps you have already taken to resolve the problem.

If it is your own server, well, we'll have to wait for someone else to post suggestions of things to check. All the servers I have run had PHP/mySQL/mail/apache already setup from the installer. (one redhat, 3 Suse, 2 FreeBSD).

-Greg

They have: 68 posts

Joined: Jun 2006

It's my own server... unfortunately.

Thanks for your help. I'll look around while i wait for more help.

Edit:
I checked my php.ini to see if i had set up the SMTP incorrectly. Here's what it says:

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

; For Win32 only.
;sendmail_from = [email protected]

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

; 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 =
'

Problem is, i'm unfamiliar with this... so if anyone can tell that this is a problem, i'd love to know. Thanks

They have: 68 posts

Joined: Jun 2006

Well, i figured out a bit more.

The server is Windows and i plugged in the separate SMTP server into the php.ini as follows:

[mail function]
; For Win32 only.
SMTP = 147.***.233.10
smtp_port = 25

; For Win32 only.
;sendmail_from = [email protected]

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

; 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 restarted the server after making the changes.. but the e-mails still don't go.

Here's an example of the test scripts i'm running:

<?php
if (isset($_POST['sendit'])) {

   
$to = \"[email protected]\"; //Change to your email
   
$subject = \"Test Email\";
   
$message = \"Hello, this is a test email.\";
   
$headers = \"From: [email protected]\";

    echo \"Mail sent, check your inbox\";
    exit;

} else {



<form  method=\"post\" action=\"
$_SERVER['PHP_SELF']; \">

<input type=\"submit\" name=\"sendit\" value=\"Send It\" />

</form>



}
?>

Does this give anyone a better idea? Thanks.

Busy's picture

He has: 6,151 posts

Joined: May 2001

There is a thread around here somewhere on how to configure the setup for emails on pc
I tried a quick search but didn't come up with it, try searching (in this forum) for things like: php email localhost server - a combination of those

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.