Welcome to WebmasterWorld Guest from 18.208.186.19

Forum Moderators: coopster & jatar k

PHP mail() script only not working on MOST browsers

php mail() submit issue

     
7:01 pm on Jul 7, 2019 (gmt 0)

New User

joined:July 7, 2019
posts: 1
votes: 0


Please forgive me if similar questions have been asked before, as I see a number of them on different forums.
I promise that I have searched for answers in help topics and php guides.

I designed a form page in Dreamweaver and then attempted to write a script in php in order for the details of the form to be sent to my email on submit.
It works on my version of Microsoft Edge (which I don't usually use so didn't realise immediately) but not on Chrome, Firefox or IE.

If you don't mind I will share the code, both for the form and the php file. I have changed the emails to false addresses but in the actual code I have used real ones of course.

Is there anything obvious which is stopping the form submitting. It is going to the php result page, but giving the message: "Sorry, there was a problem sending your message."
Only in Microsoft Edge am I receiving the message that it worked and I also receive an email as expected.

For the form, some details are added in the parameters that do not seem to be added in very basic forms, such as max and min lengths and required (due to creating in Dreamweaver) The 'required' parameters work great, so I haven't added additional check codes in the action php script, just the one which tries to stop spamming.

Form Code in the ContactUs.php file (I have tried it as an html file but made it a php when I thought I may have to add some php code within it) :

<form action="newtestacknowledge.php" method="post" enctype="text/plain" id="ContactForm">
<p style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 300; font-size: large;">
<label for="Name">*Name:</label>
<input name="Name" type="text" required="required" id="Name" form="ContactForm" size="50" maxlength="80">
<label for="email"><br>
<br>
*Email:</label>
<input name="Email" type="email" required="required" id="email" form="ContactForm" size="40" maxlength="60">
<label for="tel"><br>
<br>
Phone:</label>
<input name="Phone" type="tel" id="tel" form="ContactForm" size="30">
&nbsp;</p>
<p style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: normal; font-weight: 300; font-size: large;">
<label for="timezone">Time Zone:</label>
<select name="timezone" id="timezone" form="ContactForm">
<option>Eastern</option>
<option>Central</option>
<option>Mountain</option>
<option>Pacific</option>
<option>Alaska</option>
<option>Hawaii</option>
<option>Other</option>
</select></p>

<p style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: normal; font-weight: 300; font-size: large;">
<label for="callafter">Best Time to Call:&nbsp;</label>
<select name="callafter" id="callafter" form="ContactForm">
<option value="00:00">12.00 AM</option>
<option value="01:00">01.00 AM</option>
<option value="02:00">02.00 AM</option>
<option value="03:00">03.00 AM</option>
<option value="04:00">04.00 AM</option>
<option value="05:00">05.00 AM</option>
<option value="06:00">06.00 AM</option>
<option value="07:00">07.00 AM</option>
<option value="08:00">08.00 AM</option>
<option value="09:00" selected="">09.00 AM</option>
<option value="10:00">10.00 AM</option>
<option value="11:00">11.00 AM</option>
<option value="12:00">12.00 PM</option>
<option value="13:00">01.00 PM</option>
<option value="14:00">02.00 PM</option>
<option value="15:00">03.00 PM</option>
<option value="16:00">04.00 PM</option>
<option value="17:00">05.00 PM</option>
<option value="18:00">06.00 PM</option>
<option value="19:00">07.00 PM</option>
<option value="20:00">08.00 PM</option>
<option value="21:00">09.00 PM</option>
<option value="22:00">10.00 PM</option>
<option value="23:00">11.00 PM</option>
</select>

<label for="callbefore">to</label>
<select name="callbefore" id="callbefore" form="ContactForm">
<option value="01:00">01.00 AM</option>
<option value="02:00">02.00 AM</option>
<option value="03:00">03.00 AM</option>
<option value="04:00">04.00 AM</option>
<option value="05:00">05.00 AM</option>
<option value="06:00">06.00 AM</option>
<option value="07:00">07.00 AM</option>
<option value="08:00">08.00 AM</option>
<option value="09:00">09.00 AM</option>
<option value="10:00">10.00 AM</option>
<option value="11:00">11.00 AM</option>
<option value="12:00">12.00 PM</option>
<option value="13:00">01.00 PM</option>
<option value="14:00">02.00 PM</option>
<option value="15:00">03.00 PM</option>
<option value="16:00">04.00 PM</option>
<option value="17:00">05.00 PM</option>
<option value="18:00">06.00 PM</option>
<option value="19:00">07.00 PM</option>
<option value="20:00" selected="">08.00 PM</option>
<option value="21:00">09.00 PM</option>
<option value="22:00">10.00 PM</option>
<option value="23:00">11.00 PM</option>
<option value="00:00">12:00 AM</option>
</select>
</p>
<p style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: normal; font-weight: 300; font-size: large;">
<label for="Comments">*Comments/Questions:<br>
</label>
<textarea name="Comments" cols="55" rows="6" required="required" id="Comments" form="ContactForm"></textarea>
<br>
</p>
<p style="font-family: Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: oblique; font-weight: 300; font-size: medium;">*required</p>
<p>
<input name="reset" type="reset" id="reset" form="ContactForm" value="reset">
<input name="submit" type="submit" id="submit" form="ContactForm" formenctype="text/plain" value="submit">
</p>
</form>


Code in the destination php file linked to the form action:

<?php
if(isset($_POST['submit'])) {

$to = ' "My Name" <me@email.com>';
$visitor_email = $_POST['Email'];
$subject = 'New Submission from SYHTS Contact Form';
$message = 'Name: '. $_POST['Name'] . "\r\n\r\n";
$message .= 'Email: '. $_POST['Email'] . "\r\n\r\n";
$message .= 'Phone: '. $_POST['Phone'] . "\r\n\r\n";
$message .= 'Time Zone: '. $_POST['timezone'] . "\r\n\r\n";
$message .= 'Call After: '. $_POST['callafter'] . "\r\n\r\n";
$message .= 'but Before: '. $_POST['callbefore'] . "\r\n\r\n";
$message .= 'Comments: '. $_POST['Comments'];
$headers = "From: info@domainname.com \r\n";
$headers .="Reply-To: $visitor_email \r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
$success = mail($to, $subject, $message, $headers, '-fme@email.com');
}

function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php if (isset($success) && $success) { ?>
<h1>Thank you</h1> Your message has been sent.
<?php } else { ?>
Sorry, there was a problem sending your message.
<?php } ?>

</body>
</html>


I apologise that the code is long, but I have been through it so many times and looked at many help pages, but as it works in Microsoft Edge and I like the email form I receive I am concerned it may be a small line or slight change needed to make it compatible with all browsers but I just cannot see it.

I know some use the header: 'X-Mailer: PHP/'
Also the '-femailaddress' in the mail() command does not seem to make a difference if there or not.

I have been tasked with creating these forms and when this is sorted I will need to create two more forms for it to work with so any help is very much appreciated. Please don't send me to more help resources as I really have looked through so many.

THANK YOU!
4:59 am on July 8, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11870
votes: 245


if $success is false use php's error_get_last [php.net] for a more specific message.
6:49 am on July 8, 2019 (gmt 0)

Preferred Member from AU 

10+ Year Member Top Contributors Of The Month

joined:May 27, 2005
posts:480
votes: 22


The use of different web browsers has no effect on how that PHP script works. Just coincidence that you noticed anything.
7:07 am on July 8, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 288


if (isset($success) && $success)

The problem with this test, is that, you don't know if you reached the "mail" function point. In your code, for example, with Chrome/Firefox, the $_POST array is not initialized, so the piece of code in the - if(isset($_POST['submit'])) { - is not executed. So, the failure, is not because of the mail command returned false, but because $success is not set.

Remove:
formenctype="text/plain"

or replace it by
formenctype="application/x-www-form-urlencoded"


The use of different web browsers has no effect on how that PHP script works. Just coincidence that you noticed anything.

There are no coincidence in computing ... everything has a reason. In this case, it's simply that Chrome/Firefox are not processing text/plain encoding the same way as Edge, and PHP is not able to populate the $_POST array.

edit: indirectly related, do not forget to test / sanitize the values received by your form, to avoid malicious code injection, and other happinesses...
9:13 am on July 14, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 288


No news, good news ?