Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending HTML/PHP Form Data in mail with Attachment using PHPmailer in PHP

    • 0
    • 0
    • 0
    • 0
    • 12
    • 0
    • 0
    • 0
    • 7.00k
    Comment on it

    Hello Reader's!,If you want to send HTML/PHP form data with attachment through mail Then PHPMailer the best option for you.

    PHPMailer is one of the popular email-generating and sending libraries in the world  thou currently many popular PHP develepoment frameworks uses PHPMailer.
    A PHP email creation and transport class featuring file attachments, SMTP servers, CCs, BCCs, HTML messages, word wrap, and more. Sends email via sendmail, PHP mail(), QMail, or with SMTP.

    So lets Start ,

    First you need to download PHPMailer Click here for download or download zip file Below
    after downloading PHPmailer zip file, Now extract the file and keep this extracted file in Your Project Folder

    if you done this work nicely then create the file inside the directory

    Your Project Folder
    		-PHPMailer folder
    		-index.php //Containing your HTML Form field 
    		-apply.php //sending mail file

    Create your index.php file inside your directory

    <!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
    <form  name="form" action="apply.php" enctype="multipart/form-data" method="POST">
    	<table >
    	<tr>
    		<td><b>Candidate Name<i style="color: red; background-color: #; font-size: 20px;">*</i></b></td>
    		<td><input type="text" name="c_name" placeholder="Candidate Name" required/></td>
    	</tr>
    	<tr>
    		<td><b>E-mail ID <i style="color: red; background-color: #; font-size: 20px;">*</i></b></td>
    		<td><input type="email" name="email_id"   pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" placeholder="E-mail ID" required/></td>
    	</tr>
    	<tr>
    		<td><b>Mobile Number<i style="color: red; background-color: #; font-size: 20px;">*</i></b></td>
    		<td><input type="text" name="mob_number"  placeholder="Mobile Number" required/></td>
    	</tr>
     		<tr>																																	
    	<td><b>Attach Your CV <i style="color: red; background-color: #; font-size: 20px;">*</i></b></td>
    	<td>
    		<div class="form-group">												
    		<input type="file" style="width:100%;" name="cv" id="logo" required/></br>											
    		</div>											
    	</td>
    	</tr>																		
    	<tr>
    	    <td colspan="2" style="text-align:center"><button type="submit" name="apply" >Apply Now</button></td>
    	</tr>
    	</table>																		
    </form>
    </body>
    </html>

    after creating index.php file in  directory now need to create apply.php file in the same  directory

    <?php
    if(isset($_POST['apply'])) 
    	{
    	$message1="";
    	$c_name=$_POST['c_name']; // Contain Candidate name 
    	$email_id =$_POST['email_id']; //Contain candidate e-mail id
    	$mob_number =$_POST['mob_number']; //Contain candidate mobile number
    				
    	$message="";
    	$message .="	// create the sending mail design acording to u 
    	<table width='800' border='1' cellspacing='0' cellpadding='8' bordercolor='#CCCCCC'>      
    		<tr>        
    		      <td colspan='2' bgcolor='#CDD9F5'><strong>Candidate Details</strong></td>   			
    		</tr> 
    		<tr>        
    			<td width='168' bgcolor='#FFFFEC'><strong>Candidate Name</strong></td>        
    			<td width='290' bgcolor='#FFFFEC'>$c_name</td>      
    		</tr>      
    		<tr>        
    			<td bgcolor='#FFFFDD'><strong>E-mail ID</strong></td>        
    			<td bgcolor='#FFFFDD'>$email_id</td>      
    		</tr>
    		<tr>        
    			<td bgcolor='#FFFFDD'><strong>Mobile Number</strong></td>        
    			<td bgcolor='#FFFFDD'>$mob_number</td>      
    		</tr>						
    	 </table>";
    
    		$subject  ="Your_Mail_Subject"; //like--- Resume From Website
    		$headers  ="";
    		include("PHPMailer/PHPMailerAutoload.php"); //Here magic Begen we include PHPMailer Library.
    		include("PHPMailer/class.phpmailer.php");   
    		$mail = new PHPMailer;
    		                              // Enable verbose debug output
    		$mail->isSMTP(); // Set mailer to use SMTP
    		$mail->Host = 'smtp.gmail.com;';  // Specify main and backup SMTP servers
    		$mail->SMTPAuth = true; // Enable SMTP authentication
    		$mail->Username = 'Your_mail_id@gmail.com';// SMTP username 
    		$mail->Password = 'Your_mail_Password'; // SMTP password 
    		$mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted
    		$mail->Port = 587; 
    		$mail->SMTPDebug = 0; // TCP port to connect to
    
    		$mail->setFrom('Your_Mail_ID@gmail.com', 'Your_From_Message'); //You Can add your own From mail
    		$mail->addAddress('recipient@domain.com'); // Add a recipient id where you want to send mail 
    			
    		$mail->addAttachment($_FILES['cv']['tmp_name'],$_FILES['cv']['name']); //This line Use to Keep User Txt,Doc,pdf file ,attachment      
    
    		$mail->addReplyTo('$email'); //where you want reply from user
    		$mail->isHTML(true); 
    		$mail->Subject=''.$subject;
    		$mail->Body=''.$message;
    
    		if(!$mail->send()) 
    			{							
    			echo "<script type='text/javascript'>alert('Error in Form :- $mail->ErrorInfo!. We will Fix This soon');document.location.href='index.php';</script>";
    			}
    		else 
    			{			
    			echo "<script language='javascript'>alert('Your Resume Submitted Successfully!. We will contact you soon ');document.location.href='index.php';</script>";				
    			}
    				
    			}
    			else
    			{
    
    			$message1.= "Code Error";
    				
    			}
    ?>

     

    After done this now open your browser and access your project

    http://localhost/your_project_name

     

    Hope this will be helpful for you!!! Contact me if you facing any problem in this :)

 12 Comment(s)

  • Warning: include(PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\email\apply.php on line 30

    Warning: include(): Failed opening 'PHPMailer/PHPMailerAutoload.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\email\apply.php on line 30

    Warning: include(PHPMailer/class.phpmailer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\email\apply.php on line 31

    Warning: include(): Failed opening 'PHPMailer/class.phpmailer.php' f
  • Hello sir one error is occur while running localhost says Error in form:-SMTP connect() failed.http://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting!.We will fix this soon.

    how can solve this problem sir please help

  • Hello sir one error is occur while running localhost says Error in form:-SMTP connect() failed.http://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting!.We will fix this soon.

    how can solve this problem sir please help

  • Hello Peter,

    Hope you are doing good!!

    Actually, First you need to download PHPMailer after downloading PHPmailer zip file, Now extract the file and keep this extracted file in Your Project Folder

    Like:

    Your Project Folder
        -PHPMailer folder
        -apply.php
        -index.php

    Click on below link for download

    https://github.com/PHPMailer/PHPMailer

    @peterkachazi
  • ramgupta1368@gmail.com

    Configure isSMTP() detail correctly.

    If you are using gmail then login with your gmail account and go to setting select Forwarding and POP/IMAP tab then enable IMAP is enabled
  • Hello,
    I uploaded on server. but its is not working. its show this error. Please reply me soon.
    Error in Form :- SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting!. We will Fix This soon
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: