Page 1 of 1

Help with PHP Email Code

Posted: Thu Mar 31, 2011 12:33 pm
by mike2fast
Hi all,

I am currently designing a PHP website and i have been following some useful tutorials but one that i have followed i have come unstuck with and it wont work. If anyone could help me with the code below it would be a great help.

First of all we have the contact.html form

<html>
<head>
<title>Ajax Contact Form by Andrew Walsh</title>
<style type="text/css">
body {
margin:50px 0px; padding:0px;
text-align:center;
}

#contactarea {
width:350px;
margin:0px auto;
text-align:left;
padding:15px;
border:1px solid #333;
background-color:#eee;
font-weight: bold;
font-family: Verdana, Arial;
font-size: 12px;
}

#inputbox {
border: 1px solid #000;
width: 270;
padding: 2px;
font-weight: bold;
font-family: Verdana, Arial;
font-size: 12px;
}

#inputlabel {
font-weight: bold;
font-family: Verdana, Arial;
font-size: 12px;

}

#textarea {
border: 1px solid #000;
padding: 2px;
font-weight: bold;
font-family: Verdana, Arial;
font-size: 12px;
width:330;
}

#submitbutton {
border: 1px solid #000;
background-color: #eee;

}
</style>

<script language="javascript">

function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function sendemail() {
var msg = document.contactform.msg.value;
var name = document.contactform.name.value;
var email = document.contactform.email.value;
var subject = document.contactform.subject.value;
document.contactform.send.disabled=true;
document.contactform.send.value='Sending....';

http.open('get', 'contact.php?msg='+msg+'&name='+name+'&subject='+subject+'&email='+email+'&action=send');
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();

if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];

}
}
}
</script>
</head>
<body>
<div id="contactarea">
<form name="contactform" id="contactform">
<span id="inputlabel">Name:</span> &nbsp;&nbsp;<input type="text" name="name" id="inputbox"><br /><br />
<span id="inputlabel">Email:</span> &nbsp;&nbsp;&nbsp;<input type="text" name="email" id="inputbox"><br /><br />
<span id="inputlabel">Subject:</span> <input type="text" name="subject" id="inputbox"><br /><br />
<span id="inputlabel">Message:</span><br />
<textarea name="msg" rows="10" id="textarea"></textarea>
<br /><br />
<input type="button" value="Send Email" name="send" onclick="sendemail();" id="submitbutton">

</form>
</div>
</body>
</html>

Then we have the contact.php form

<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "mike2fast135@hotmail.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action']))
{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

/* Now lets trim up the input before sending it */

$name = trim($_GET['name']); //The senders name
$email = trim($_GET['email']); //The senders email address
$subject = trim($_GET['subject']); //The senders subject
$message = trim($_GET['msg']); //The senders message

mail($to,$subject,$message,"From: ".$email.""); //a very simple send

echo 'contactarea|Thank you '.$name.', your email has been sent.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
?>

From what i can gather the HTML form passes the information to the PHP form which then sends the information to your email address. I have changed the email address to the one that i want to use and have also tried using a different one incase it did not like the one i have chosen to use.

If anyone could help with this i would really appreciate it.

Thanks

Re: Help with PHP Email Code

Posted: Thu Mar 31, 2011 3:30 pm
by mecha_godzilla
When you say it doesn't work, what happens exactly? No emails received?

The first thing you should do is make sure that the mail() operation returned TRUE (to confirm that the operation completed). The easy way to do this is change the way you use mail() in your script so it looks like this:

Code: Select all

$mail_sent_status = @mail($to, $subject, $message, $headers);

if ($mail_sent_status) {
    echo 'Mail sent';
} else {
    echo 'Mail not sent';
}
What mail() does in PHP is pipe (output) your message to sendmail (or sometimes Postfix, which pretends that it's sendmail) and sendmail then assumes responsibility - as the mail transfer agent - for sending your message.

This arrangement applies to Un*x/Linux systems so if you're working on a Windows server then some other application will be being used. If you have root access to your server you can check the mail logs to see what's happening (this would confirm if the problem is with your script or the sendmail set-up). What you can also do is save your compiled message to a text file (or just echo() it out to the browser) to make sure that it looks correct.

Also, normally you'd create a separate variable called $header and then append any fields that you need to it, like this

Code: Select all

$header = 'From: ' . $email;
$header .= 'Reply-To: ' . $email;
and then call the mail() function like this

Code: Select all

mail($to, $subject, $message, $headers);
to make it easier to keep track of what's going on.

HTH,

Mecha Godzilla

Re: Help with PHP Email Code

Posted: Thu Mar 31, 2011 7:31 pm
by mike2fast
i just dont recieve any email messages could there be something which i have overlooked in the code? Been trying to get this working now for a while but not had much look i am quite new to using PHP.

Re: Help with PHP Email Code

Posted: Fri Apr 01, 2011 3:24 pm
by mecha_godzilla
Here are some suggestions:

1. Write a really basic mailer script and see if it works - don't even try receiving any values from your form, just use in-script values for your To: address, subject, message and headers. The PHP manual entry for mail() gives the following example:

Code: Select all

$to      = 'nobody@example.com'; // put your email address here, obviously!
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
2. Very occasionally, the mailer fails because of the type of email address you're using. Try and use a dial-up email address in addition to any web mail addresses you've got. Having said that, emails not being sent is almost always a problem with the message format or headers so your script is likely to be the problem. When I was first started using mail() I tried out a range of example scripts that I found on the 'net and a few didn't work - the script looked fine but the headers weren't properly formed.

3. Use phpinfo() to make sure a path for sendmail has been configured - if your server is provided as part of a normal hosting package then this will almost certainly be the case.

HTH,

M_G