If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Nothing is jumping out at me as being out of place. The important thing is to discover who is not receiving your emails. If I understand you correctly, some people are receiving your emails and others are not? Or are you saying that some of the messages are not being sent out at all?

Thanks for your reply, James.
In fact, this is the code of a form on my website, on which people register : https://www.lichaamengeest.be/nieuw.php
Several people filled in the form, then got to the "thank you" page (meaning the form has been sent), but I never receive the mail.
So maybe it doesn't work properly when images are too large, or maybe some other reason?

You need to log, either to a text file or a database table, the date/time, entered email address, ip address, and the result (success or error messages.) This will give you a record of form submissions that you can compare to the emails you receive.

This code is lacking several features that will either cause the emails to be sent or to tell you why they are not and it is open to spammers sending their emails through your mail server, due to mail header injection, so the problem could be that your sending mail server and/or the receiving mail server, where the To: email address is located at (even if these are the same mail server), is refusing to send/receive the emails, is placing them into the junk/spam folder, or is temporarily getting blacklisted/banned.

The most likely problem for it only working some of the time/for some submitted email addresses, is because the email address that someone entered in the form is NOT where these emails are being sent from. These emails are being sent from your web hosting and the From: mail header should either be an email address at your web site domain or at a domain where there is an SPF (Sender Policy Framework) record that indicates your sending mail server is authorized to send email for that domain.

You can put the email address that was entered in the form into a Reply-to: mail header, after validating that it is exactly and only one email address.

Next, this form processing code should do the following -

1) Detect that a post method form has been submitted before referencing any of the form data.

2) Because this is uploading files, if the total size of the submitted form data is greater than the post_max_size setting, both the $_POST and $_FILES arrays will be empty. The code should detect this condition, output a user error message 'Sorry, the form data was too large, try again with smaller files...', log the date/time, entered email address, ip address, submitted data size, and prevent the remainder of the code from running. You should also look at and set the post_max_size and upload_max_filesize settings to be reasonable values that you are willing to accept from the people filling in the form.

3) Validate all the submitted form data before using it. At a minimum, any 'required' fields should not be empty and the entered email address should be a single correctly formatted address. For validating the uploaded files, you need to check the ['error'] element in the $_FILES data and only use the uploaded file if there is no error. You also need to decide what you are going to do for each of the possible upload errors. Some of the errors can be corrected by the user (an aborted/partial upload, file too large.) For these errors, you can output an error message telling the user to re-try the form submission, and list the reason for the error. For the errors that are caused by the server setup, you should output a generic error message, that the uploaded files could not be used, and log the actual errors, so that you can find and fix what is causing them.

4) If there are no validation errors, you can use the submitted form data. When using the text values (form fields and uploaded file name and type), you need to apply htmlentities() to each value so that any html/javascript that could be in the value won't be rendered when the email is read.

5) If the mail() call fails, again, you need to log the date/time, email address, ip address, and your own mail fail message, so that you can try to find and fix any problem. You should also have php's error_reporting set to E_ALL and log_errors set to ON. This will cause the php error information from the mail() call to be logged too.

6) Even if the mail() call succeeds, there is no guarantee that the email will get sent by the sending mail server or accepted by the receiving mail server. You should log the data/time, email address, ip address, a success message, the total number of uploaded files and either the size of each file or the total size of all the files, and any other values you think may be helpful. This will tell you that there was a form submission and hopefully give information that will help find out why you are not receiving some of the emails.

If you will be using a file for logging the information, php's file_put_contents(), with the FILE_APPEND flag, is the simplest method.

$ip = $_SERVER['REMOTE_ADDR']; // get ip to short variable name for logging purposes

Replace the existing if(mail(...)) ... else ... code with the following -

PHP Code:

if(mail($to, $subject, $message, $headers))
{
echo "Dankje om je gegevens door te zenden.<br>We nemen die door en contacteren je zo snel mogelijk.";
// 6) Even if the mail() call succeeds, there is no guarantee that the email will get sent by the sending mail server or accepted by the receiving mail server. You should log the data/time, email address, ip address, a success message, the total number of uploaded files and either the size of each file or the total size of all the files, and any other values you think may be helpful. This will tell you that there was a form submission and hopefully give information that will help find out why you are not receiving some of the emails.
// log the success information to compare against actual received emails
_log($log_file,$ip,$email,'Mail call successful.');

Thanks for that, DyDr.
Just implemented your code, and all works fine, thanks.
But what about the log.txt file? Is that a file I just upload and gets filled whenever a mail is sent?

And this. You wrote:

// 6) Even if the mail() call succeeds, there is no guarantee that the email will get sent by the sending mail server or accepted by the receiving mail server. You should log the data/time, email address, ip address, a success message, the total number of uploaded files and either the size of each file or the total size of all the files, and any other values you think may be helpful. This will tell you that there was a form submission and hopefully give information that will help find out why you are not receiving some of the emails.

Does that mean the form code is still not waterproof?
What to add of code in order to "log the data/time, email address, ip address, a success message, the total number of uploaded files and either the size of each file or the total size of all the files"?

The last post you made is even worse. The code is out of proper order, which will produce php errors and won't work at all, but beyond that, unless your web site is hosted at google, I can guarantee that you won't be able to send email to an @gmail account because the From: mail header doesn't correspond to your web hosting and gmail isn't going to accept an email that states it is From: a gmail email address if wasn't actually sent from a gmail mail server.

The suggestions I have made in this thread are to get your code to either work (fix the obviously wrong From: email address usage and to only attempt to send an email when there's valid data to use) or to log information that would help find why it isn't working. Until you find the reason the existing code isn't working, you don't have anything to fix, and making random changes is just a waste of time.

You will either need to implement the suggestions or hire someone to do so. Programming help forums as not free programming services and the amount of time it would take to design, write, test, and debug all the code needed is more that you should expect someone to do for you for free.

The only additional suggestion I have is to find or write a script that uses either the phpmailer or swiftmailer php class. This will simplify the code needed to build the multi-part mime message w/file attachments, the mail headers, and will allow you to use SMTP authentication against your receiving email account, which will help to guarantee that the email will be accepted, though it can still end up in the junk/spam folder.