21
03
Jquery Ajax PHP Contact us form with attachment sending Mail Using PHPMailer

In this article, we will make a contact form with attachment for your website. There are only required fields in the contact form in which every website that needed. The contact form need to have two parts : the client side front end and the server side back end. The client side of the form is normally coded with xhtml, javascript and styles while the server side of the form is coded with php. In this form we will show you how to use PHPMailer for sending the email with attachment of files. PHPMailer is one of the most and best popular open source PHP libraries  to send email.

Jquery Ajax contact form hackandphp blog

Javascript Form Validation:

function validateContact() {
	var valid = true;	
	$(".demoInputBox").css('background-color','');
	$(".info").html('');
	
	if(!$("#userName").val()) {
		$("#userName-info").html("(Name required)");
		$("#userName").css({'background-color':'#FFFFDF',"border-color":"red"});
		valid = false;
	}
	if(!$("#userEmail").val()) {
		$("#userEmail-info").html("(Email required)");
		$("#userEmail").css({'background-color':'#FFFFDF',"border-color":"red"});
		valid = false;
	}
	if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
		$("#userEmail-info").html("(Email invalid)");
		$("#userEmail").css({'background-color':'#FFFFDF',"border-color":"red"});
		valid = false;
	}
	if(!$("#subject").val()) {
		$("#subject-info").html("(Subject required)");
		$("#subject").css({'background-color':'#FFFFDF',"border-color":"red"});
		valid = false;
	}
	if(!$("#content").val()) {
		$("#content-info").html("(Content required)");
		$("#content").css({'background-color':'#FFFFDF',"border-color":"red"});
		valid = false;
	}
	
	return valid;
}

});
$(document).on('change', '.btn-file :file', function() {
    var input = $(this),
        numFiles = input.get(0).files ? input.get(0).files.length : 1,
        label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
		$("#fileName").text(label);
    input.trigger('fileselect', [numFiles, label]);
})

Sending an E-mail with attachment

Let us see an example how to send email with attachments using PHPMailer

require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP(); // Enable SMTP debugging. 
$mail->SMTPDebug = 0; //Set PHPMailer to use SMTP.
$mail->SMTPAuth = TRUE; //Set this to true if SMTP host requires authentication to send email
$mail->SMTPSecure = "tls"; //If SMTP requires TLS encryption then set it
$mail->Port     = 587;  
$mail->Username = "xyz@gmail.com."; //Provide Username
$mail->Password = "yyyy";  //Provide Password
$mail->Host       = "smtp.gmail.com"; //Set SMTP host name  
$mail->Mailer   = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("hackandphp@gmail.com");	
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;
$mail->MsgHTML($_POST["content"]);

To add the attachment we just call the function name AddAttachment by the PHPMailer object by passing the file path as the argments.

$mail->AddAttachment($targetPath); // Provide file path and name of the attachments 

Using SMTP

SMTP is a protocol used for sending and receiving email between servers. Once the mail server verifies the email it sends it to the destination mail server.

Here we have used the Gmail's account as the mail server. Gmail requires TLS encryption over SMTP so we set it accordingly. Before you send via SMTP, you need to find out the host name, port number, encryption type if required and if authentication is required you also need the username and password. The most important things while using Gmail's account as the mail server is we have to turn on the less secure apps. Follow the below step how to turn on the less secure app:

  • 1. Sign in to gmail
  • 2. Go to Myaccount
  • 3. Then go to sign-in & security.
  • 4. At the bottom of the page we can see (allow less secure apps) just turn it be on.

If you are a PHP developer, there is little chance of avoiding having to send emails programmatically. While you may opt for third party services like Mandrill or Sendgrid, sometimes that just isn’t an option, and rolling your own email sending library even less so. That’s where PHPMailer and its alternatives (Zend Mail, Swiftmailer, etc..) come in.

By posted on - 21st Mar 2016