Log in

View Full Version : Resolved How can i add HTML functionality to this PHP contact Script?



Ryan Fitton
08-12-2010, 07:14 PM
Hi there, I already have a PHP script that sends an email to me when someone used the contact form on my website, but the email I receive is just standard text, how can i add hyperlinks and images into this script? My script is below:


<?php

function domain_exists($email,$record = 'MX')
{
list($user,$domain) = split('@',$email);
return checkdnsrr($domain,$record);
}

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}


// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('Europe/London');


// get posted data into local variables
$EmailFrom = "ryan.fitton@googlemail.com";
$EmailTo = "ryan.fitton@googlemail.com";
$Subject = "Email From Personal Website";
$YourName = Trim(stripslashes($_POST['YourName']));
$YourEmail = Trim(stripslashes($_POST['YourEmail']));
$YourMessage = Trim(stripslashes($_POST['YourMessage']));
$YourIP = Trim(stripslashes($_POST['IP']));
$Date = date("d M Y");
$Time = date("h:i A");


// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
exit;
}


// Check if required fields are filled in
if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
exit;
}


// check for a valid email
if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
die("<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">");
else {


// prepare email body text
$Body .= "Ryan Fitton, you have recieved an email from the contact form on your website.";
$Body .= "\n\n";
$Body .= "This message was sent on the ";
$Body .= $Date;
$Body .= " at ";
$Body .= $Time;
$Body .= "\n\n";
$Body .= "--------------------------------------";
$Body .= "\n";
$Body .= "IP Adresses: ";
$Body .= $IP=$_SERVER['REMOTE_ADDR'];
$Body .= "\n\n";
$Body .= "Name: ";
$Body .= $YourName;
$Body .= "\n\n";
$Body .= "Email: ";
$Body .= $YourEmail;
$Body .= "\n\n";
$Body .= "Message: ";
$Body .= $YourMessage;
$Body .= "\n";
$Body .= "--------------------------------------";
$Body .= "\n\n\n";
$Body .= "This email has arrived from http://www.ryanfitton.co.uk and is private for Ryan Fitton (ryan.fitton@hotmail.co.uk). If you are not Ryan Fitton and you have recieved this email, please contact me using this email address: ryan.fitton@hotmail.co.uk";


// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_sent.php\">";

}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
}

}

?>

I want to add an hyperlink using html for example:

<a href='http://www.google.co.uk'>Clike here to access Google</a>

I want to have this somewhere in the body tags, such as:

$Body .= "Ryan Fitton, you have recieved an email from the contact form on your website here: <a href='http://www.google.co.uk'>Clike here to access Google</a>";

bluewalrus
08-12-2010, 11:14 PM
You need to give it a header



$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: <$EmailFrom>\r\n";
$success = mail($EmailTo, $Subject, $Body, $headers);


http://php.net/manual/en/function.mail.php

Ryan Fitton
08-13-2010, 07:30 AM
Hi thanks for your gel but i've managed to get it working using this:

<?php

function domain_exists($email,$record = 'MX')
{
list($user,$domain) = split('@',$email);
return checkdnsrr($domain,$record);
}

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}


// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('Europe/London');


// get posted data into local variables
$EmailFrom = "ryan.fitton@googlemail.com";
$EmailTo = "ryan.fitton@googlemail.com";
$Subject = "Email From Personal Website";
$YourName = Trim(stripslashes($_POST['YourName']));
$YourEmail = Trim(stripslashes($_POST['YourEmail']));
$YourMessage = Trim(stripslashes($_POST['YourMessage']));
$YourIP = Trim(stripslashes($_POST['IP']));
$Date = date("d M Y");
$Time = date("h:i A");


// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
exit;
}


// Check if required fields are filled in
if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
exit;
}


// check for a valid email
if(!isValidEmail($YourEmail) || !domain_exists($YourEmail))
die("<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">");
else {


// prepare email body text
$Body .= "Ryan Fitton, you have recieved an email from the contact form on your website <a href='http://www.ryanfitton.co.uk'>here</a>:";
$Body .= "\n\n";
$Body .= "This message was sent on the ";
$Body .= $Date;
$Body .= " at ";
$Body .= $Time;
$Body .= "\n\n";
$Body .= "--------------------------------------";
$Body .= "\n";
$Body .= "IP Adresses: ";
$Body .= $IP=$_SERVER['REMOTE_ADDR'];
$Body .= "\n\n";
$Body .= "Name: ";
$Body .= $YourName;
$Body .= "\n\n";
$Body .= "Email: ";
$Body .= $YourEmail;
$Body .= "\n\n";
$Body .= "Message: ";
$Body .= $YourMessage;
$Body .= "\n";
$Body .= "--------------------------------------";
$Body .= "\n\n\n";
$Body .= "This email has arrived from http://www.ryanfitton.co.uk and is private for Ryan Fitton (ryan.fitton@hotmail.co.uk). If you are not Ryan Fitton and you have recieved this email, please contact me using this email address: ryan.fitton@hotmail.co.uk";

$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1' ;
$headers[] = 'From: <'.$EmailFrom.'>';

// the implode is because each line has to be separated by an return space (part of the RFC)
$success = mail($EmailTo, $Subject, $Body, implode("\r\n", $headers));

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_sent.php\">";

}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact_error.php\">";
}

}

?>

bluewalrus
08-13-2010, 12:12 PM
That's the same thing with an extra function, the implode.

Ryan Fitton
08-15-2010, 02:54 PM
Yeahh :) i thought it was but I wasn't sure :P