0

I'm trying to run a php script on a website contact form. It's probably worth mentioning it's from a website template I bought and I have designed the website using this. My html/css/php knowledge is 'absolute beginner level' hence why I am on here...

Below is the php script (this came with the template). However it's not sending email through to the recipient email address. I've been told it's because the script is trying to send email from an external domain (ie the email address of the website visitor) through the domainname.co.uk mail server, and it’s going to reject it - how can I edit this script so that it works?

This is the PHP script:

<?php
session_start();

$email_to = 'enquiries@bonnelhomes.co.uk'; // change with your email
$name     = $_POST['name'];  
$email    = $_POST['email'];
$subject   = $_POST['subject'];
$message    = $_POST['message'];

$headers  = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";

if(mail($email_to, $subject, $message, $headers)){
    echo "success";       
} 
else{
    echo "failed";     
}  

This is the html for the contact form:

<form id="contact" class="row" name="form1" method="post" action="send.php"  >

                        <div class="span4">
                            <label>Name</label>
                            <input type="text" class="full" name="name" id="name" />
                        </div>

                        <div class="span4">
                            <label>Email <span class="req">*</span></label>
                            <input type="text" class="full" name="email" id="email" />
                            <div id="error_email" class="error">Please check your email</div>
                        </div>

                        <div class="span8">
                            <label>Message <span class="req">*</span></label>
                            <textarea cols="10" rows="10" name="message" id="message" class="full"></textarea>
                            <div id="error_message" class="error">Please check your message</div>
                            <div id="mail_success" class="success">Thank you. Your message has been sent.</div>
                            <div id="mail_failed" class="error">Error, email not sent</div>

                            <p id="btnsubmit">
                                <input type="submit" id="send" value="Send" class="btn btn-large" />
                            </p>
                        </div>


                    </form>

Any help would be much appreciated. Many thanks in advance :o)

Federkun
  • 36,084
  • 8
  • 78
  • 90
Loobs520
  • 1
  • 1

3 Answers3

1

replace

$headers  = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";

with

$headers  = "From: mail@yourdomain.com\r\n";

UPDATE
since it is a contact form don't forget to add your user's details to $message

$message = $name. "<br>" .$email. "<br>" .$message;
N Kumar
  • 1,302
  • 1
  • 18
  • 25
  • Hi, Thanks for your help. I tried changing the code to this: $headers = "From: website@bonnelhomes.co.uk\r\n"; but it still did not work, is there something I am missing? Thanks. – Loobs520 Oct 04 '15 at 08:36
  • @Loobs520 are you checking `inbox` as well as `spam` of `enquiries@bonnelhomes.co.uk` ???? – N Kumar Oct 04 '15 at 17:05
  • HI, I have only been talking with the hosting company who tell me they are not receiving - so not sure if it was being marked as spam, but in the meantime I have got the php script to work by doing as you said but also I had to rewrite some of the html... Many thanks for your help, really appreciated :o) – Loobs520 Oct 05 '15 at 17:39
0

If you still have problems sending "from your server" you might let some real email-server do the work. There is some Framework you can use which is called PHP-Mailer. To use it, you have to download the framework and to place it into your server. Using this, you might wanna use SMTP (login-information from some real email-account). it would look like: FTP-Folderview

require './PHPMailer/PHPMailerAutoload.php';

$mailer = new PHPMailer;

here you configure your email account to send the emails from. look on your hosters help-files to find out what you need to use to log in successfully:

$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = 'smtp.strato.de';
$mailer->Username = 'your@sendingaccount.de';
$mailer->Password = 'xxxxxxx';
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;

$mailer->From = 'your@sendingaccount.de';
$mailer->FromName = 'Mr Tester';

here you configure your actual email, you want to send:

$mailer->addAddress('enquiries@bonnelhomes.co.uk','Mr Admin');  
$mailer->Subject = 'this is a contactform email';
$mailer->AltBody = 'your text with bla and request');

if($mailer->send()){
    echo 'yeah man!';
}else{
    echo 'some error occured';
}   

Normally this is not absolutly necessary but it helps on servers with sendingproblems or if your emails get blockt by spam blockers.

Nibbels
  • 156
  • 10
  • Hi Thanks for your help - I am a completely novice when it comes to web coding so this does not mean much to me. Is there any way I can edit my existing php script to work through a mail server? All I get when trying to send through the contact form is 'LOADING.." and then it just hangs.... Many thanks for your help! – Loobs520 Oct 04 '15 at 13:49
0

security tip, on this line:

$email_to = 'enquiries@bonnelhomes.co.uk';

has security problem. An attacker can modify the headers, message and use your server to send unlimited spam messages to victims.

so add this line for more security:

if (strlen($email_to) > 30 || $email_to !== 'enquiries@bonnelhomes.co.uk') {
exit("Bye Hacker!");
}
Red Acid
  • 217
  • 1
  • 3
  • 14