PHP SENDING MAIL WITH ATTACHEMENTS
----------------------------------------------------------------------------------------------------------------------------------
We can send mails with attachements using 3 ways
1. PHP MAILER
2. PEAR
3. SWIFT MAILER
Here we are using PHP MAILER functions to send a mail with attchment.
Step 1:
We need to download PHP mailer .
Place it in your project folder
Step 2:
Creating a form with file upload input type(index.php)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form name="career_form" action="upload_file.php" method="post" enctype="multipart/form-data">
First Name<br/>
<input type="text" name="txt_first_name" id="txt_first_name"><br/>
Last Name<br/>
<input type="text" name="txt_last_name" id="txt_last_name"><br/>
Contact Number<br/>
<input type="text" name="txt_number" id="txt_number"><br/>
Email<br/>
<input type="text" name="txt_email" id="txt_email"><br/>
Cover letter<br/>
<textarea name="txt_cover_letter" rows="10" cols="30"></textarea><br/>
Upload resume
<input type="file" name="txt_resume" id="txt_resume"><br/><br/>
<input type="submit" name="btn_send_resume" value="Submit">
</form>
</body>
</html>
Step 3:
Submit action page(upload_file.php)
<?php
// Including the class.phpmailer.php in phpmailer folder to our action page
// Including the class.phpmailer.php in phpmailer folder to our action page
require_once './mail/class.phpmailer.php';
if(isset($_REQUEST['btn_send_resume']))
{
$txt_first_name = $_REQUEST['txt_first_name'];
$txt_last_name = $_REQUEST['txt_last_name'];
$name = $txt_first_name." " .$txt_last_name;
$txt_number = $_REQUEST['txt_number'];
$txt_email = $_REQUEST['txt_email'];
$txt_cover_letter = $_REQUEST['txt_cover_letter'];
$txt_job_title = "Career openings in mublesolutions";
// HTML message in the mail
$message = '<html>
<head>
</head>
<body>
<div style="margin:auto;width:90%;padding:30px 10px">
<div style="border: 1px solid #E9E9E9; min-height: 600px;width:100%;padding:10px">
<hr style="border: 1px solid #E9E9E9; padding:0px 10px ">
<br/>
<h4 style="color:51A8B;">Dear Recruiter,</h4>
<p>One candidate is applied for the job <b>'.$txt_job_title.'</b>,Candidate details are listed below </p>
<table style="text-align: left;border-bottom:1px solid #20629C;width:100%">
<tr style="line-height: 40px;background-color:#20629C;color:white;">
<th width="300" colspan="2">Candidate details</th>
</tr>
<tr style="line-height:30px; ">
<th>First name </th>
<td> '.$txt_first_name.'</td>
</tr>
<tr style="line-height:30px;">
<th>Last name</th>
<td> '.$txt_last_name.'</td>
</tr>
<tr style="line-height:30px;">
<th>Email ID</th>
<td> '.$txt_email.'</td>
</tr>
<tr style="line-height:30px;">
<th>Contact Number</th>
<td> '.$txt_number.'</td>
</tr>
</table>
<h4>Cover letter</h4>
<p>'.$txt_cover_letter.'</p>
<h4>Candidate resume is attached along with this mail</h4>
<hr style="border: 1px solid #E9E9E9; padding:0px 10px "><br/>
</div>
</div>
</body>
</html>';
// Checking file is uploaded and sending mail
if(isset($_FILES['txt_resume']))
{
$file_name = $_FILES['txt_resume']['name'];
$temp_name = $_FILES['txt_resume']['tmp_name'];
$details = pathinfo($file_name);
$file = $details["filename"];
$extension = $details['extension'];
$rand_digit = rand(0000, 9999);
$allowed_extension = array("pdf", "doc", "docx","odt","txt");
if (($_FILES["file_employee_resume"]["size"] < 20000000) && in_array($extension, $allowed_extension))
{
if ($_FILES["file_employee_resume"]["error"] > 0)
{
$response = "error";
}
else
{
$target_path = "uploads/";
$new_file_name = $file.$rand_digit.".".$extension;
if(move_uploaded_file($temp_name,$target_path.$new_file_name))
{
//Sending mail using php mailer
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try
{
$mail->AddAddress('aryapramesan@gmail.com', 'John Doe'); // To address
$mail->SetFrom($txt_email,$name ); // From address
$mail->Subject = 'Application received against your Job Offer:'.$txt_job_title;
$mail->MsgHTML($message); // Html messages if any can added using this method can read a file also
$mail->AddAttachment("uploads/".$new_file_name); // attachment
$mail->Send();
$response = "success";
unlink($target_path.$new_file_name); //Removing file uploaded to uploads folder
}
catch (phpmailerException $e)
{
$response = $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
$response = $e->getMessage(); //Boring error messages from anything else!
}
}
else
{
$response = "move";
}
}
}
else
{
$response = "extension";
}
echo $_SESSION['msg'] = $response;
}
}
?>
We need to create an upload folder to upload file to our server after sending the mail we can unlink the file from our upload folder
$mail->MsgHTML(file_get_contents('content.html'));
We can red content from an html file and set it as our html message
No comments:
Post a Comment