Various errors when trying to send mail

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
acecombat
-


Joined: 29 Sep 2005
Posts: 31

PostPosted: Tue Mar 18, 2008 12:22 pm    Post subject: Various errors when trying to send mail Reply with quote

I am using the following script to send E-Mail via a web script as I am designing a legacy application which has no socket connection to be able to E-Mail out, and the only way I can do it is via a web script. It just needs to be a simple quick script with fields like: To, From (Name), Subject, Message.

I have tried the following code I found on the net but kept getting an "Invalid email address" and when editing the section out that checks email address in the script I was getting a 500 internal server error.

I have attached the code below but if anyone has a better one or can tell me how to fix my one I would appreciate it. I have also included the logs from Abyss when both the above errors occur

Mail Script constists of mail.html and mail.php

mail.html:
Code:
<html>
<head><title>Mail sender</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>
</body>
</html>


mail.php:
Code:
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
  echo "<h4>Thank you for sending email</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>


Abyss error log:
Code:
18/Mar/2008:22:19:14 +1100   SUID: 0   PUID: 0   RUID: 0   URI:    Reading 0 bytes failed = The process cannot access the file because it is being used by another process.
18/Mar/2008:22:19:14 +1100   SUID: 0   PUID: 0   RUID: 0   URI:    timeout-header2!
PHP Notice:  Undefined variable: HTTP_POST_VARS in C:\Abyss Web Server\htdocs\test\mail.php on line 7
PHP Notice:  Undefined variable: HTTP_POST_VARS in C:\Abyss Web Server\htdocs\test\mail.php on line 8
PHP Notice:  Undefined variable: HTTP_POST_VARS in C:\Abyss Web Server\htdocs\test\mail.php on line 9


---After removing validation---

PHP Parse error:  syntax error, unexpected T_ELSEIF in C:\Abyss Web Server\htdocs\test\mail.php on line 12
Back to top View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Tue Mar 18, 2008 11:49 pm    Post subject: Re: Various errors when trying to send mail Reply with quote

acecombat wrote:
I have tried the following code I found on the net but kept getting an "Invalid email address" and when editing the section out that checks email address in the script I was getting a 500 internal server error.
Code:
PHP Parse error:  syntax error, unexpected T_ELSEIF in C:\Abyss Web Server\htdocs\test\mail.php on line 12


You did remove all of the if statement, including the else?
Because that might be the problem, try removing all of the following, does that work?
Code:
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}

_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
acecombat
-


Joined: 29 Sep 2005
Posts: 31

PostPosted: Wed Mar 19, 2008 8:28 am    Post subject: Reply with quote

Yes I removed that and to no avail, then I noticed the mail line has an elseif as a branch of the first if statement so I changed it to an if statement also to no avail.

Code:
elseif (mail($email,$subject,$message)) {
Back to top View user's profile Send private message
acecombat
-


Joined: 29 Sep 2005
Posts: 31

PostPosted: Wed Mar 26, 2008 5:13 am    Post subject: Reply with quote

Anyone have any idea?
Back to top View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Wed Mar 26, 2008 1:03 pm    Post subject: Reply with quote

acecombat wrote:
Anyone have any idea?


Code:
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];


instead of $HTTP_POST_VARS try $_POST instead

Code:
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
rrinc
-


Joined: 24 Feb 2006
Posts: 725
Location: Arkansas, USA

PostPosted: Wed Mar 26, 2008 8:29 pm    Post subject: Reply with quote

I agree with that. You could also use $_REQUEST if you don't care if its from GET or POST. HTTP_* variables are being removed from PHP in version 6.
_________________
-Blake | New Server :D
SaveTheInternet
Soy hispanohablante. Puedes contactarme por mensajes privados.
Back to top View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group