Hi, If you are looking for the PHP script to learn how to send email with attachment in PHP without going to spam then you are in the right place. I know you searched a lot on Google to find source code but every code sends that to spam, not in the inbox. Check our demo to confirm.
Here I am going to give a PHP script by which your email with an attachment will always go to inbox. Sending an email with attachment is the most common task in web development.
When you are applying for a job or submitting any application, you often might be noticed that they accept a file to upload. It may be anything like your CV or resume, profile picture or any other document.
Sending details to email is sometimes good in terms of saving space on our server. Since we are not storing the details on our database or attachment to our server folders.
How to do that?
Here we will use a simple HTML form accepting basic details like name, email, subject, message, and an attachment. The attachment has a validation that only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload. In the previous tutorial, we learned about how to send email in PHP step by step but here we will do the same with attachment.
So what are we waiting for? Let’s start. Check the files and folder we are going to create.
index.php is the main file which is a simple HTML form as I told above.
email-script.php is the script file that sends an email with an attachment.
validation-script.js is a javaScript validation file.
uploads folder saves the attachment to send emails and deletes later.
Now we should create these files. I am giving you just code block here. Make sure you put these CDN in to head off your index.php to work your code.
functionvalidateEmailSendForm(){var name = $("#name").val(); var email = $("#email").val(); var subject = $("#subject").val(); var message = $("#message").val(); var attachment = $("#attachment").val(); if(name == ""){ $("#nameError").show(); $("#nameError").html("Please enter your name"); $("#nameError").fadeOut(4000); $("#name").focus(); returnfalse; }elseif(email == ""){ $("#emailError").show(); $("#emailError").html("Please enter your email"); $("#emailError").fadeOut(4000); $("#email").focus(); returnfalse; }elseif(!validateEmail(email)){ $("#emailError").show(); $("#emailError").html("Please enter valid email"); $("#emailError").fadeOut(4000); $("#email").focus(); returnfalse; }elseif(subject == ""){ $("#subjectError").show(); $("#subjectError").html("Please enter subject"); $("#subjectError").fadeOut(4000); $("#subject").focus(); returnfalse; }elseif(message == ""){ $("#messageError").show(); $("#messageError").html("Please enter some message"); $("#messageError").fadeOut(4000); $("#message").focus(); returnfalse; }elseif(attachment == ""){ $("#attachmentError").show(); $("#attachmentError").html("Please select a attachment"); $("#attachmentError").fadeOut(4000); $("#attachment").focus(); returnfalse; }else{returntrue; }functionvalidateEmail(inputText){var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/; if(inputText.match(mailformat)){returntrue; }else{returnfalse; }}
}
function validateEmailSendForm() { var name = $("#name").val(); var email = $("#email").val(); var subject = $("#subject").val(); var message = $("#message").val(); var attachment = $("#attachment").val(); if (name == ""){ $("#nameError").show(); $("#nameError").html("Please enter your name"); $("#nameError").fadeOut(4000); $("#name").focus(); return false; }else if (email == ""){ $("#emailError").show(); $("#emailError").html("Please enter your email"); $("#emailError").fadeOut(4000); $("#email").focus(); return false; }else if (!validateEmail(email)){ $("#emailError").show(); $("#emailError").html("Please enter valid email"); $("#emailError").fadeOut(4000); $("#email").focus(); return false; }else if (subject == ""){ $("#subjectError").show(); $("#subjectError").html("Please enter subject"); $("#subjectError").fadeOut(4000); $("#subject").focus(); return false; }else if (message == ""){ $("#messageError").show(); $("#messageError").html("Please enter some message"); $("#messageError").fadeOut(4000); $("#message").focus(); return false; }else if (attachment == ""){ $("#attachmentError").show(); $("#attachmentError").html("Please select a attachment"); $("#attachmentError").fadeOut(4000); $("#attachment").focus(); return false; }else{ return true; } function validateEmail(inputText) { var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/; if(inputText.match(mailformat)) { return true; } else{ return false; } }
}
function validateEmailSendForm() { var name = $("#name").val(); var email = $("#email").val(); var subject = $("#subject").val(); var message = $("#message").val(); var attachment = $("#attachment").val(); if (name == ""){ $("#nameError").show(); $("#nameError").html("Please enter your name"); $("#nameError").fadeOut(4000); $("#name").focus(); return false; }else if (email == ""){ $("#emailError").show(); $("#emailError").html("Please enter your email"); $("#emailError").fadeOut(4000); $("#email").focus(); return false; }else if (!validateEmail(email)){ $("#emailError").show(); $("#emailError").html("Please enter valid email"); $("#emailError").fadeOut(4000); $("#email").focus(); return false; }else if (subject == ""){ $("#subjectError").show(); $("#subjectError").html("Please enter subject"); $("#subjectError").fadeOut(4000); $("#subject").focus(); return false; }else if (message == ""){ $("#messageError").show(); $("#messageError").html("Please enter some message"); $("#messageError").fadeOut(4000); $("#message").focus(); return false; }else if (attachment == ""){ $("#attachmentError").show(); $("#attachmentError").html("Please select a attachment"); $("#attachmentError").fadeOut(4000); $("#attachment").focus(); return false; }else{ return true; } function validateEmail(inputText) { var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/; if(inputText.match(mailformat)) { return true; } else{ return false; } }
}
Run the code!
Now it’s time to run and test our code. If you did everything step by step then you will not face any errors and you will get output something like this.
This form is validated properly like empty fields, valid email and attachment file accepted. When you fill that form and hit the submit button then you will get the alert message of success or errors.
Attention !!! It may take 1 or 2 minutes to receive email with attachment so please have patience. But I am sure you will receive in primary inbox not to spam.
I hope you learned explained above, If you have any suggestions, are appreciated. And if you have any errors comment here. You can download the full 100% working source code from here.
Ok, Thanks for reading this article, see you in the next post.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok