Page 1 of 1
Mail Form Problem
Posted: Wed May 13, 2009 6:16 am
by naseemsadak
Hi,
I'm trying to create a mail form in php but when I try to send an email to the recipient email address, it does not go through. I tried using an example from
http://www.w3schools.com but it does not work. Here is the code.
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 "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("naseemsadak@yahoo.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='mailform3.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
</body>
</html>
I would appreciate any help.
Thanks,
Naseem
Re: Mail Form Problem
Posted: Wed May 13, 2009 8:48 pm
by ldougherty
What happens when you run the script?
Try using a simple mail() statement like such to see if this works for you..
http://www.hostmysite.com/support/linux ... g/phpmail/
If it does not then post up your code and let us know what the result is.
Re: Mail Form Problem
Posted: Thu May 14, 2009 2:49 am
by naseemsadak
I realize the mistake in the code above and what I forgot to add was an else statement so instead of it just saying "Thank you for using our mail form", it would have given an error message to say the email was not sent. I still can't get this mail form working. I tried this code and it says "Mail send failure - message not sent ". I uploaded the script on the web hosting service I am using and executed it. Here's the link -
http://phpworks.uuuq.com/mailform4.php
Here's my code:-
Code: Select all
<html>
<body>
<?php
$to = 'naseemsadak@gmail.com';
$subject = 'the subject';
$from = 'naseemsadak@yahoo.com';
$message = 'hello';
if(mail($to, $subject, $message, "From: $from"))
echo "Mail sent";
else
echo "Mail send failure - message not sent"; ?>
</body>
</html>
Re: Mail Form Problem
Posted: Thu May 14, 2009 2:55 am
by naseemsadak
Is there a difference between "
user@domain.com" and "
email@domain.com" or are they both the same as in putting email addresses for "$to" and "$from", from the article link you provided me with?
Re: Mail Form Problem
Posted: Sat May 16, 2009 6:58 am
by naseemsadak
I have figured the problem as to why the email is not being sent as I did not configure a php.ini script with smtp server configuration. I have looked at some forums and tutorials but still confused. I would appreciate if anyone could show me a simple way of doing this so that it works with the code I posted in this post e.g being able to send an email from the mail form to my gmail address. Thanks.
Re: Mail Form Problem
Posted: Sat May 16, 2009 12:30 pm
by ldougherty
Is this a windows server or linux server? In linux you do not need to specify the SMTP server in your php.ini as it uses the sendmail functionality in linux.
On a Windows server you should designate the SMTP server you use in your php.ini by adjusting the values for these lines..
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
sendmail_from =
me@example.com
Re: Mail Form Problem
Posted: Sat May 16, 2009 12:38 pm
by naseemsadak
It's for windows. The "sendmail_from" will it be your email address used for receiving emails from the mail form? And where do you execute the php.ini script in the mail form script?
Re: Mail Form Problem
Posted: Sat May 16, 2009 12:49 pm
by naseemsadak
Or rather how does the php.ini script executes? I know I have to test the mailform script on the web hosting service I am using so if I just upload the php.ini script on the hosting service, will my mailform automatically detect the php.ini script?
Re: Mail Form Problem
Posted: Sat May 16, 2009 12:54 pm
by ldougherty
You don't execute or replace the php.ini, the php.ini is the configuration file for PHP.
If you just adjust the values in the php.ini used by your system then mail will work.
If you do not know the php.ini being used create an info page with phpinfo(); in it and look for your php.ini location there.
Re: Mail Form Problem
Posted: Sun May 17, 2009 4:47 am
by naseemsadak
where does the php.ini file go in windows?
Re: Mail Form Problem
Posted: Sun May 17, 2009 5:15 am
by naseemsadak
the php.ini file located in C:\Program Files\PHP, is that the file that needs to be edited in order for your smtp configuration to work? And if so, it does not need to be copied to C:\Windows? I am getting this part when running phpinfo() for the config file which I don't understand - /etc/php-users.ini
Re: Mail Form Problem
Posted: Sun May 17, 2009 5:27 am
by naseemsadak
I located the mail function settings in the php.ini file stored in the php folder located in C:\program files but when i uploaded my mail form script and tried executing it, it still does not work.
Re: Mail Form Problem
Posted: Sun May 17, 2009 4:36 pm
by naseemsadak
I have been able to send an email via localhost to the recepient email address. I used this tutorial link that helped -
http://drupal.org/node/30079 . What I want to know, is now I want to test my mail script on the web hosting service I am using. In order for me to do this, what exactly do i need to do? Do I upload the php.ini to my web hosting account and call it in the mailform script for the mail form script to work?
Re: Mail Form Problem
Posted: Mon May 18, 2009 3:28 am
by naseemsadak
Since no one has replied, as mentioned I have been able to get the php mail() function to work using the smtp virtual server and I have found out that the web hosting service I use may not allow smtp out going mail (I have posted a question on the zymic forum, hopefully someone will get back to me). If anyone knows another free web hosting service that offers smtp outgoing mail, I'd appreciate if you can let me know. Thanks.