Copy link to clipboard
Copied
Hi,
I'm new to the IT world so please bear with me. Ive been at this problem for 10 hours and I like finding answes on my own but I admit do failure.
I have this html code for the contact form;
<form class="contact_form" action="kontakt.php" method="post">
<p><input type="text" required="required" id="contact_name" name="contact_name" class="text_input" value="" size="22" />
<label for="contact_name">Namn *</label></p>
<p><input type="text" required="required" id="contact_company" name="contact_company" class="text_input" value="" size="22" />
<label for="contact_company">Företag *</label></p>
<p><input type="email" required="required" id="contact_email" name="contact_email" class="text_input" value="" size="22" />
<label for="contact_email">Epost *</label></p>
<p><textarea required="required" name="contact_content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info@webelite.se" name="contact_to"/>
</form>
Now the problem is that it apears I suck at PHP, the best I could do was get the form to send emails but they where all blank.
This is all the php I could gather;
<?php
$contact_name = $_POST['contact_name'];
$contact_company = $_POST['contact_company'];
$contact_email = $_POST['contact_email'];
$contact_content = $_POST['contact_content'];
$mail_to = 'info@webelite.se';
$subject = 'Lilla form '.$field_name;
$body_message = 'From: '.$contact_name."\n";
$body_message .= 'E-mail: '.$contact_email."\n";
$body_message .= 'Message: '.$contact_content;
$headers = 'From: '.$contact_email."\r\n";
$headers .= 'Reply-To: '.$contact_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
<?php
}
?>
I really need the PHP ready so please help me....
Thank you
Copy link to clipboard
Copied
There are a lot of things you need to take into account with PHP forms, security being one of the biggest. If you don't know how to ensure your form is not used by hackers and spammers, you should probably use a premade script until you have a better grasp on PHP.
There are many available online for free, and they usually include a step-by-step tutorial to implement them.
I suggest Formmail at http://www.tectite.com
EDIT: One of the added benefits of using a pre-made script is you can have it up and running in a few minutes and can then take your time learning PHP to create your own script at your own pace.
Copy link to clipboard
Copied
Thank you for your answer but if I dont do this then I wont understand how it works. I just want to know why its sends blank mails.
I only get;
From:
Subject:
Copy link to clipboard
Copied
Most of your code is OK, but there are two errors:
$subject = 'Lilla form '.$field_name;
The variable $field_name is not defined. This might cause problems with the way the mail is sent.
At the bottom of your code, you have this:
<?php
}
?>
Remove the opening PHP tag and the closing curly brace.
However, as Jon points out, your script is very vulnerable to hackers and spammers. You should never use the input of a form for the subject line or headers of an email without first checking it.
As long as your server is using PHP 5.2 or higher, you can use filter_input() to check the email like this:
$contact_email = filter_input(INPUT_POST, 'contact_email', FILTER_VALIDATE_EMAIL);
Copy link to clipboard
Copied
Thank you for helping me, I will study more about spam filters etc. This is still just a test run, the form is sending the email but the body_messages are empty when i check my Email.
It only show this:
Company:
Email:
Content:
If you or someone only could help find out why this is happening I could move to the next step.
Thank you very much
Copy link to clipboard
Copied
oke I need to validate the email maybe, did not try that yet?
Copy link to clipboard
Copied
could anyone tell me how I validate my emails?
Copy link to clipboard
Copied
I tested your original code without actually sending an email. It works correctly.
This is what I did:
// Comment out the line that sends the mail for testing purposes
// $mail_status = mail($mail_to, $subject, $body_message, $headers);
// Display the contents gathered by the form
echo $body_message . '<br>';
echo $headers;
Both the $body_message and $headers displayed the correct values.
Once you make sure the correct values are being displayed, you can remove the two slashes at the beginning of the line that sends the mail.
Trying to test the PHP mail() function from a local computer frequently fails because of security restrictions imposed by hosting companies. Test the script from your remote server (live website).
If it doesn't work on your remote server, it's possible that your hosting company requires the fifth argument for mail(). This is normally a string consisting of -f followed immediately by your email address:
$mail_status = mail($mail_to, $subject, $body_message, $headers, '-fme@example.com');
Another possible problem is that you're using the email address submitted in the form as the From header. The From header should indicate the origin of the message, which is your website, not an arbitrary address entered in a form field.
As for validating email addresses, I gave you the code for that before, using the filter_var() function.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now