Send mail with attachment using PHPMailer

Post Reply
User avatar
hiccup
Site Admin
Posts: 17
Joined: Sat Aug 01, 2015 8:44 pm

Send mail with attachment using PHPMailer

Post by hiccup » Mon Mar 13, 2017 7:39 pm

Code: php Select all

<?php

$field_first_name = $_POST['contact_names'];
$field_email = $_POST['contact_email'];
$field_front = $_POST['optionsRadios'];
$field_just_sold = $_POST['optionsRadios'];
$field_back = $_POST['optionsRadios2'];
$field_property_address = $_POST['property_address'];
$field_price = $_POST['price'];
$field_number_of_postcards = $_POST['number_of_postcards'];
$field_upload_files = $_POST['upload_files'];
$subject = 'Message from a site visitor '.$field_first_name;

$body_message = 'From: '.$field_first_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Front: '.$field_front."\n";
$body_message .= 'Back: '.$field_back."\n";
$body_message .= 'property_address: '.$field_property_address."\n";
$body_message .= 'price: '.$field_price."\n";
$body_message .= 'number_of_postcards: '.$field_number_of_postcards . "\n";


$max_allowed_file_size = 1024; // size in KB

$size_of_uploaded_file = $_FILES["attachment"]["size"]/1024;//size in KBs

if($size_of_uploaded_file > $max_allowed_file_size ){?>
    <script language="javascript" type="text/javascript">
		alert("Size of file should be less than <?php echo $max_allowed_file_size; ?>");
		window.location = 'index.html';
	</script>
	<?php
	die();
}

$file_type = explode("/", $_FILES['attachment']['type']);
if (!( !strcasecmp($file_type[0], "image") )){?>
    <script language="javascript" type="text/javascript">
		alert("Only image files are allowed!");
		window.location = 'index.html';
	</script>
	<?php
    die("Only image files are allowed!");
}


require_once 'PHPMailerAutoload.php';

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = $field_first_name;

//To address and name
// $mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("your_email"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
// $mail->addCC("[email protected]");
// $mail->addBCC("[email protected]");

// Attachments
$mail->addAttachment($_FILES['attachment']['tmp_name'] , $_FILES['attachment']['name']);
//Send HTML or Plain Text email
$mail->isHTML(false);

$mail->Subject = $subject;
$mail->Body = $body_message;
// $mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) { ?>
    <script language="javascript" type="text/javascript">
		alert("Mailer Error: <?php echo $mail->ErrorInfo; ?>");
		window.location = 'index.html';
	</script>
	<?php
    // echo "Mailer Error: " . $mail->ErrorInfo;
} 
else { ?>
    <script language="javascript" type="text/javascript">
		alert("Message has been sent successfully");
		window.location = 'index.html';
	</script>
	<?php
    //echo "Message has been sent successfully";
}

HTML:

Code: html Select all

<section class="Section1">
      <div class="container">
        <form class="form-horizontal" method="post" action="contact.php" enctype='multipart/form-data'>
            <div class="front_area">
                <div class="row">
                    <h2>front</h2>
                    <div class="col-sm-6">
                            <div class="checkbox">
                                <label>
                                  <input type="radio" name="optionsRadios" id="optionsRadios1" value= "option1"> <img src="1.jpg" alt="img"/>
                                  <h3>option1</h3>
                                </label>
                              </div>
                    </div>
                    <div class="col-sm-6">
                            <div class="checkbox">
                                <label>
                                  <input type="radio" name="optionsRadios" id="optionsRadios2" value= "option2"> <img src="2.jpg" alt="img"/>
                                  <h3>option2</h3>
                                </label>
                              </div>
                    </div>
                  </div>
                  

            </div>
            
            <div class="back_area">
                <div class="row">
                    <h2>Back</h2>
                    <div class="col-sm-6">
                            <div class="checkbox">
                                <label>
                                  <input type="radio" name="optionsRadios2" id="optionsRadios3" value="option3"> <img src="3.jpg" alt="img"/>
                                  <h3>Option3</h3>
                                </label>
                              </div>
                    </div>
                    <div class="col-sm-6">
                            <div class="checkbox">
                                <label>
                                  <input type="radio" name="optionsRadios2" id="optionsRadios4" value="option4"> <img src="Images/4.jpg" alt="img"/>
                                  <h3>option4</h3>
                                </label>
                              </div>
                    </div>
                  </div>
            </div>
            
            
            
            <div class="main_form_area">
                <div class="form_start">
                  <div class="form_items">
                      <h5><span>Customer</span></h5>
                     <div class="form-group">
                            <label for="inputname" class="col-sm-2 control-label">Name<sup>*</sup></label>
                            <div class="col-sm-10">
                              <input type="text" class="form-control" required name="name" id="inputname" placeholder="">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputemail" class="col-sm-2 control-label">Email<sup>*</sup></label>
                            <div class="col-sm-10">
                              <input type="email" class="form-control" required name="email" id="inputemail" placeholder="">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputaddress" class="col-sm-2 control-label">Address<sup>*</sup></label>
                            <div class="col-sm-10">
                              <input type="text" class="form-control" required name="address" id="inputaddress" placeholder="">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputprice" class="col-sm-2 control-label">Price<sup>*</sup></label>
                            <div class="col-sm-10">
                              <input type="text" class="form-control" required name="price" id="inputprice" placeholder="">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputinvestment" class="col-sm-2 control-label">Other<sup>*</sup></label>
                            <div class="col-sm-10">
                              <select class="form-control" required name="number_of_postcards" id="inputinvestment">
                                  <option style="background:#b1b1b1;">select an option</option>
                                  <option>Option6</option>
                                  <option>Option7</option>
                                  <option>Option8</option>
                                </select>
                            </div>
                          </div>
                          
                          
                          <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                              <p>Upload a Picture of yourself</p>
                               <div class="input-group input-group2">
                                  <div class="input-group-addon">
                                  <input type="file" name="attachment" id="exampleInputFile" value="Browse...">
                                  </div>
                                </div>
                            </div>
                          </div>
                          
                          
                          <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                              <div class="upload_image_btn">
                                    <input type="file" id="upload_image" name="upload_files">
                                    <button>Upload</button>
                                  </div>
                            </div>
                          </div>
                          
                          
                          
                          <div class="form-group submit_btn">
                            <div class="col-sm-offset-2 col-sm-10">
                              <button type="submit" class="btn btn-default">Submit</button>
                            </div>
                          </div>
                
                  </div>
                
                </div>
            </div>
            
        </form>
      </div>
  </section>
  
    <script src="Js/jquery.min.js"></script> 
    <script src="Js/bootstrap.min.js"></script>

Post Reply