Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
Hi! I have some problems with my php contact form. My hosting supports this php contact form because it is working great but if I use in php file another mail address as receiver, for the example my gmail, yandex mail or another not free mail from another hosting, the form doesn't work. The only way that the contact form works is when the mail is from hosting which I upload my site on it. Do you have any recommendation to fix other mails with contact form? 


What I have tried:

$emailTo = '1234@gmail.com';
$siteTitle = 'company.';

error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

//If the form is submitted
if(isset($_POST['submitted'])) {

// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'Forgot your name!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}

// upon no failure errors let's email now!
if(!isset($hasError)) {

$subject = 'New message to '.$siteTitle.' from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
$headers = 'From: ' .' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);

//Autorespond
$respondSubject = 'Thank you for contacting '.$siteTitle;
$respondBody = "Your message to $siteTitle has been delivered! \n\nWe will answer back as soon as possible.";
$respondHeaders = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;

mail($email, $respondSubject, $respondBody, $respondHeaders);

// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
Posted
Updated 30-May-16 14:31pm
Comments
Sergey Alexandrovich Kryukov 30-May-16 20:24pm    
Not true. The form works the same way. For the mechanism of sending HTTP request via the form and handling it with PHP, e-mail is just yet another string; there is nothing which would prevent passing parameters. The problem could be in mail sending itself. You need to correct your observations, debug code or use logging, mimic sending the mail with the same parameters using some other mail program, and so on.
—SA
[no name] 10-Aug-18 4:23am    
Check your mail method in the php contact form. You must have some file like contact_mail.php in which you can specify mail where your contact form data will be sent. It should look something like this:

<?php
$toEmail = "name@gmail.com";
$mailHeaders = "From: " . $_POST["your_name"] . "<". $_POST["your_email"] .">\r\n";
if(mail($toEmail, $_POST["comments"], $_POST["your_phone"], $mailHeaders)) {
echo"Contact Mail Sent.";
} else {
echo"Problem in Sending Mail.";
}
?>

Source: Creating custom PHP Contact form for beginners.

1 solution

Please see my comment to the question. The idea for you is: the problem is unrelated to "working form". Chances are, if your code really works with some addresses, the problem is unrelated with you code at all. The code has much worse problem though.

You are very lucky that you failed to put your code in production so far. When and if you fix the problem, you can consider your whole site dead, as soon as someone decides to attack it. The code is so unsafe that even the beginner's malicious activity will turn your site's host a zombie sending spam, or something like that, in no time. For the explanation, please see my past answer: unable to send mail , it showing the error in below code .[^].

The fix is also very simple, but needs some logical analysis and accurate work.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900