Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

PHP send a mail with pdf attachment example

PHP send a mail with pdf attachment example

Today, I will give you an example of “How to send a mail with pdf attachment in PHP“, Sometimes we need to collect user information and document in career form or contact query form in the admin email from the website we collect user information with some attachments.

First, what we’re doing here, This is the example :

php send email with attachment example

php send mail with pdf attachment example

php send email with attachment example

In the given below example, We will create an Html form where we will create a form to submit all the user data with any type of document (an image, document, or pdf ) File.

After these steps, we will create a PHP file to perform an action and send all the user-related data into the email.

Let’s start PHP send email with attachment example

career.html


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Upload Resume and send mail in PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
  <style>
    a {
    color: #BE206B!important;
    }
    a.btn.btn-lg.btn-block,.btn-info {
    color: white !important;
    }
    .btn-secondary {
    background-color: #448BC6!important;
    color: #fff!important;
    }
    button.btn.btn.btn-secondary {
    width: 100%;
    }
    h3 {
    text-align: center;
    line-height: 200%;
    }
    .collpa
    .pt-0, .py-0 {
    padding-top: 0!important;
    }
 </style>
</head>
<main>
  <br><br>
  <div class="container">
     <div class="row justify-content-center">
        <div class="col-lg-6">
          <form method="POST" action="careersubmit.php" enctype="multipart/form-data">
           <div class="main">
           
            <h3><a>Upload Resume and send mail in PHP </a></h3>
            <br>
         <div class="row">
            <div class="col-sm-6">
              <label class="form-label fw-bold">Name <span class="text-danger">*</span></label>
              <input type="text" name="name" class="form-control" placeholder="Enter Your Name" required>
            </div>
            <div class="col-sm-6">
              <label class="form-label fw-bold">Email ID <span class="text-danger">*</span></label>
              <input type="text" name="email" class="form-control" placeholder="Enter Your Email ID" required>
            </div>
            </div>
            <br>
            <div class="row">
              <div class="col-sm-6">
              <label class="form-label fw-bold">Position <span class="text-danger">*</span></label>
              <input type="text" name="position" class="form-control" placeholder="Enter position" required>
            </div>
            <div class="col-sm-6">
              <label class="form-label fw-bold">Mobile<span class="text-danger">*</span></label>
              <input type="text" name="phone" class="form-control" placeholder="Enter Your Mobile No." required>
            </div>
            </div>
            <br>
            <div class="row">
          <div class="col-sm-6">
              <label class="form-label fw-bold">Total
                Experience <span class="text-danger">*</span></label>
                <div class="row">
                  <div class="col-sm-4">
                    <select name="total_exp_year" class="wpcf7-form-control wpcf7-select
                    wpcf7-validates-as-required form-select years_box required"
                    aria-required="true" aria-invalid="false" required>
                    <option value="" selected="selected">Years</option>
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value=">25">&gt;10</option>
                  </select>
                  </div>
                  <div class="col-sm-4"> 
                    <select name="total_exp_month" class="wpcf7-form-control wpcf7-select
                    wpcf7-validates-as-required form-select years_box required"
                    aria-required="true" aria-invalid="false" required>
                    <option value="" selected="selected">Months</option>
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="10">11</option>
                  </select>
                  </div>
                </div>
            </div>
            <div class="col-sm-6">
              <label class="form-label fw-bold">Upload Resume <span class="text-danger">*</span></label>
              <div class="form-group files">
                <input type="file" name="file" class="form-control" required>
            </div>
        </div>
        </div>
        <div class="modal-footer justify-content-start">
          <button type="submit"  name="submit" class="btn btn-danger rounded-pill btn-md px-3">Apply For Job</button>
        </div>
      </div>
      </form>
    </div>
    </div>
    </div>
</div>
</html>

careersubmit.php

<?php

    $filenameee =  $_FILES['file']['name'];
    $fileName = $_FILES['file']['tmp_name']; 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $position = $_POST['position'];
    $total_exp_year = $_POST['total_exp_year'];
    $total_exp_month = $_POST['total_exp_month'];
    
    
    $message ="Name = ". $name . "\r\n Email = " . $email . "\r\n Phone =" . $phone . "\r\n Position =" . $position . "\r\n Total experience year =" . $total_exp_year . "\r\n Total experience months =" . $total_exp_month; 
    
    $subject ="Candidate Email From Career Form";
    $fromname ="8bityard.com : Candidate Details";
    $fromemail = 'noreply@8bityar.com'; 

    $mailto = 'youremail@gmail.com';  //the email which u want to recive this email

    $content = file_get_contents($fileName);
    $content = chunk_split(base64_encode($content));
    $separator = md5(time());
   $eol = "\r\n";

    // main header (multipart mandatory)
    $headers = "From: ".$fromname." <".$fromemail.">" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

    //SEND Mail
    if (mail($mailto, $subject, $body, $headers)) {
        echo '<script> alert("your request has been submited successfully, We will Contact you Soon!"); </script>';
        
      } else {
         echo '<script> alert("Some problem occured while connection. Please try again after some time!"); </script>';
    }
?>

In this article, we learned PHP send a mail with pdf attachment example, I hope this article will help you with your PHP application Project.

Read also :- PHP echo year copyright in Footer.