How to create (and use) an email form

Status
Not open for further replies.

compositor

New Member
Messages
7
Reaction score
0
Points
1
Hi,

I am currently using the free web hosting product and have decided to create a PHP web form, however while I am able to receive emails from other accounts, nothing comes from the web form submission.

I believe I first need to have an email address that must be on the x10 hosting server however is there any difference in relation to the domain I use, i.e. my own (domain) or a sub-domain with x10 hosting?

Thanks for any help
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hi,

Yes, if you are specifying a "From" header in your form, you would need to set it to an email account that exists on your free hosting account. You can add email addresses from your control panel. They can be suffixed with any domain that is present on your account. :)

I'm struggling to find a hosting account linked with your forum account, though. Please could you link your accounts by going to "Support" at the top of cPanel? Sometimes our mail filters are a bit strict, so I can check if there are any problems with the emails your script is sending. ;)

Thank you,
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
there is a $email_from variable but that is for the person filling out the form.

the $email_to variable contains an email address that is hosted by x10hosting in my hosting account (not on this email address)

I got this working with another web host so the problem is not the script I'm afraid and yes, x10hosting is a bit strict when it comes to emails so I will await a fix (if there is one)

Cheers
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hi,

Please could you link your accounts, as I described above, so that I can check on this? :)

Thank you,
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
Thank you for getting back to me.

As I say, the hosting account and forum account are different so can I PM you the details?

Otherwise I am quite happy to publish here.

Cheers
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The sender (From) needs to be an email address on your account, and that is almost certainly what your $email_from variable is trying to set to an external address (the user's email address). That's "spoofing", and that's not allowed here. (Spoofing can be used for all sorts of nefarious shennanigans, and has often gotten the x10Hosting servers placed on spam blocklists in the past.)

If you need to be able to reply to the user from your email client, you can use the ReplyTo header instead. The mail will be sent from your email account, perhap to your email account, but when you reply to the message, the reply will go to the user's email address.
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
<?php
if(isset($_POST['email'])) {
$email_to = "";
$email_subject = "Enquiry";

function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br><br>";
echo $error."<br><br>";
echo "Please go back and fix these errors.<br><br>";
die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$event = $_POST['event']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br>';
}
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br>';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br>';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Event: ".clean_string($event)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<?php
}
?>
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
$email_to I thought was meant to be the email address with x10hosting so would I need to include an additional variable for the Reply to header and are these standard variable names in PHP?

The form worked with another hosting but for some reason my account was terminated (I prefer X10 actually but this form business...)

Anyway, hope I can get this going.

Regards
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
This line isn't allowed in its current form, since it sets the From header to the user's email address, and since the mail isn't actually being sent from the user's email address, that's "spoofing":
PHP:
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

It needs to be modified to read:
PHP:
$headers = 'From: '.$email_to."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

(That assumes that the value in $email_to is a valid address on your account.) I don't see where you're setting $email_from to a valid address, but you've presumably just left that out. You can still reply to the user by virtue of the ReplyTo header.
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
Hello,

I changed the PHP script to be how you said but nothing in my email account.

Is there supposed to be something in Cpanel that enables mail to be sent to a user account?
 

compositor

New Member
Messages
7
Reaction score
0
Points
1
Hello,

I changed the PHP script to be how you said but nothing in my email account.

Is there supposed to be something in Cpanel that enables mail to be sent to a user account?
 
Status
Not open for further replies.
Top