Page 1 of 1

mail form problems

Posted: Fri May 22, 2009 9:51 am
by naseemsadak
Hi,

Yet again I am having more problems. I am having problems sending a message with the mail form using different email addresses in the "e-mail header". If I use my gmail address, the mail form sends the message to the recepient email address but if I use my yahoo or hotmail addresses, the mail form does not send the message to the recepient email though I get a message to say it has been successfully sent. To give an e.g. take this to be my mail form layout with the following:-

name : naseem
e-mail: naseemsadak@gmail.com(my gmail address)
message: your message goes here

now when I click the submit button the message gets sent through but when I use my hotmail or yahoo email address in place of the gmail address, I still get the message to say it's sent but it's not received. I am testing the php script using xampp and the smtp server relay configuration in IIS from the link that helped me to do so http://drupal.org/node/30079 . Here is my mail form code in php:-

Code: Select all

<html>
<body>
<?php
function spamcheck($field)
{
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
 
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
}
  if (IsSet($_REQUEST['email']))
  {//if "email" is filled out, proceed
 
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if($mailcheck==FALSE)
    {
    echo "email address is invalid";
    }
    //send email
 
else
 {
    $ToEmail = 'sadak@worldonline.co.za';
    $EmailSubject = 'Contact Form ';
    $mailheader = "From: ".$_REQUEST['email']."\r\n";
    $mailheader .= "Reply-To: ".$_REQUEST['email']."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY .= nl2br($_REQUEST['comment'])."<br>";
 
    if(IsSet($_REQUEST['email']))
    {
       mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader);
    }
    
    if (!mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
    {
       echo "Error in sending message. Please ensure details are entered correctly";
    }
    
    else
    {
       echo "Message was successfully sent";
    }
 }
?>
 
<?php
}else{
?>
<form action="contactform.php" method="post">
<table><tr>
<td colspan="2" align="center"><b>Contact Us</b><p></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">Name:</td>
<td valign="top"><input name="name" id="name" size="25"></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">E-mail:</td>
<td valign="top"><input name="email" id="email" size="25"></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">Comments:</td>
<td valign="top" overflow="scroll"><textarea name="comment" rows="5" cols="35" id="comment"></textarea></td>
</tr><tr>
<td colspan="2" align="center" class="bodytext"><input type="submit" name="Submit" value="Send" >
<input type="reset" value="Reset" name="reset"></td>
</tr></table>
<?php
};
?>
</body>
</html>
 
this is my configuration for mail function:-

Code: Select all

 
[mail function]
; For Win32 only.
SMTP = 127.0.0.1(I use this IP address to send an email from my pc)
smtp_port = (this can be either 25 or 465 as both helped in sending an email with my gmail address)
 
; For Win32 only.
sendmail_from = sadak@worldonline.co.za
 
Another problem I have is when I just test my mail form script alone, it sends an email once but when I call up my mail form in my html menu script, the email is being sent twice instead of one time. I used the $isset function to ensure the email is not sent twice. Here is my code in menu script:-

Code: Select all

<html>
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 
<style type="text/css">
 
#menu { 
  padding:0; 
  margin:0; 
}
 
#menu ul {
list-style: none;
<!--margin: 0;
padding: 0;-->
border: none;
} 
 
#menu li { 
  list-style-type:none; 
}
 
#menu li a {
display: block;
text-decoration: none;
color: #fff;
border-left: 1px solid white;
border-right: 1px solid white;
width: 90px;
height: 25px;
border: 1px solid white;
}
 
<!--#menu li a:hover{
color: #fff;
}-->
 
 
</style>
</head>
<body bgcolor="#0000CD">
 
<div id="menu">
<ul>
<li><font size="2" color="white"><a href="http://localhost/Welcome_Page.html" target="mainframe"><center>Home Page</center></a></font></li>
<li><font size="2" color="white"><a href="http://localhost/Company_Profile.html" target="mainframe"><center>Profile</center></a></font></li>
<li><font size="2" color="white"><a href="http://localhost/map_information.html"  target="mainframe"><center>Directions</center></a></font></li>
<li><font size="2" color="white"><a href="http://localhost/contactform.php" target="mainframe"><center>Contact us</center></a></font></li>
</ul>
</div>
 
</body>
</html>
Any help would be appreciated.

Thanks,

Naseem

Re: mail form problems

Posted: Fri May 22, 2009 5:49 pm
by nyoka
I know it sounds daft but have you checked your spam folders?

Re: mail form problems

Posted: Sat May 23, 2009 6:03 am
by naseemsadak
no it doesn't come in the spam folder. Man this mail form is driving me crazy :crazy:

Re: mail form problems

Posted: Sat May 23, 2009 7:59 am
by anand
naseemsadak wrote:no it doesn't come in the spam folder. Man this mail form is driving me crazy :crazy:
It happened to me as well. It is because hotmail and yahoo have blocked all mails from your server.

Upload script in some free server and try sending mail, it'll pass through.

Re: mail form problems

Posted: Sat May 23, 2009 8:25 am
by naseemsadak
Most free web hosting services don't offer free smtp/pop3 service (such as zymic which I use) so that's why I'm testing my script with xampp. I'm still persisting in trying to get this to work and I used the phpmailer() example and I have been able to send an email to any email address using for example my gmail account settings to send an email from:-

Code: Select all

<?php
// This example will send mail through the SMTP server with authentication.
require("class.phpmailer.php"); // be sure to change this to your location!
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "someone@gmail.com"; // GMAIL username
$mail->Password = "*******"; // GMAIL password
$mail->From = "someone@gmail.com";
$mail->FromName = "your name";
$mail->AddAddress("someone@domain.com"); //can put any recepient email addresses here
$mail->Subject = "Test PHPMailer Message";
$mail->Body = "Hi! \n\n This was sent with phpMailer_example3.php.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
this worked for me but now what I would like to know if it is possible, how can I use this phpmailer() example in the form tag e.g. <form action="phpmailer.php"............ as this example requires smtp authentication and it must be able to understand what email address is being used with it's respective smtp server or what can I extract from here and put it in my contactform example posted here as well to get it to use any email address in the e-mail header and be able to send the email through?

Re: mail form problems

Posted: Mon May 25, 2009 3:00 pm
by naseemsadak
At last I got something to work after :banghead: for hours. Only one problem I haven't resolved and it's do with getting an "else" statement if there is an error in sending the form, an error message should appear, but if the form is blank or you put an incorrect email address and you click "submit", it will just go on showing the form but if you put put the correct details, the message will be sent through. Here's my code for anyone who might be having a similar problem as well (this is tested with xampp :D ):-

Code: Select all

<html>
<body>
<?php
function spamcheck($field)
{
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
 
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
}
 
  $EmailSubject = ' Contact Form ';
  $name_field = $_REQUEST['name'];
  $email_field = $_REQUEST['email'];
  $mailheader = "From: $name_field <$email_field>";
  $MESSAGE_BODY .= nl2br($_REQUEST['comment'])."<br>";
  $mail_check = spamcheck($email_field);
 
   if (isset($name_field))
   {
      $name = TRUE;
   }
 
   else
   {
      $name = FALSE;
   }
 
   if (isset($email_field) && $mail_check == TRUE)
   {
      $email = TRUE;
   }
 
   else
   {
      $email = FALSE;
   }
 
   if (isset($MESSAGE_BODY))
   {
      $message = TRUE;
   }
 
   else
   {
      $message = FALSE;
   }
 
 
   if($name == TRUE && $email == TRUE && $message == TRUE)
   {
     $Decision = TRUE;
 
    if($Decision == TRUE)
    {
    mail('someone@domain.com', $EmailSubject, $MESSAGE_BODY, $mailheader);
    echo "Message was successfully sent";
    }
 
 
?>
 
<?php
}else{
?>
<form action="contactform.php" method="post">
<table><tr>
<td colspan="2" align="center"><b>Contact Us</b><p></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">Name:</td>
<td valign="top"><input name="name" id="name" size="25"></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">E-mail:</td>
<td valign="top"><input name="email" id="email" size="25"></td>
</tr><tr>
<td valign="top" align="right" class="bodytext">Comments:</td>
<td valign="top" overflow="scroll"><textarea name="comment" rows="5" cols="35" id="comment"></textarea></td>
</tr><tr>
<td colspan="2" align="center" class="bodytext"><input type="submit" name="Submit" value="Send" >
<input type="reset" value="Reset" name="reset"></td>
</tr></table>
<?php
};
?>
</body>
</html>
 
As mentioned, I could not get an error message to display as I know the problem is coming from the line of code "if($name == TRUE && $email == TRUE && $message == TRUE)........" as the else statement for this if statement embeds the mail form
"}else{?><form action="contactform.php" method="post">.........". I haven't been able to work around this and if anyone has an idea or solution, I would appreciate if you could show me.

Thanks,

Naseem

Re: mail form problems

Posted: Tue May 26, 2009 7:27 am
by naseemsadak
Problem solved! Just seperated the form code from the php code and put them in separate files one being a php script and the other an html file and called up the php script in the html file.