0

I am using phpmailer for contact form. I added an attachment field for upload file/image in contact form using . But i can not integrate php functions for file attachment. I am weak in php, but this is very important for me to add this in my contact form.

Please someone help me. My form code and php code are given below. Thanks in advance.


//** This is html markup

<form action="mobileapp.php" method="post" enctype="multipart/form-data">

 <h2>Your Contact Info</h2>
  <p>Your First Name* <br />
    <input type="text" name="firstName" id="firstName" required />
  </p>
  <p>Your Last Name* <br />
    <input type="text" name="lastName" id="lastName" required />
  </p>
  <p>Your Email* <br />
    <input type="email" name="email" id="email" required />
  </p>
  <p>Upload Your logo<br />
    <input type="file" name="uploaded_file" id="uploaded_file"> 
  </p>
 </form>

//* This is the phpmailer code

  <?php

  /* config start */

  $emailAddress = 'test@yourmail.com';

  /* config end */


  require "class.phpmailer.php";


  $msg=
  'First Name:'.$_POST['firstName'].'<br />
  Last name:'.$_POST['lastName'].'<br />
  Email:'.$_POST['email'].'<br />


  ';


  $mail = new PHPMailer();
  $mail->IsMail();

  $mail->AddReplyTo($_POST['email'], $_POST['name']);
  $mail->AddAddress($emailAddress);
  $mail->SetFrom($_POST['email'], $_POST['name']);
  $mail->Subject = "Subject";
  $mail->MsgHTML($msg);
  $mail->Send();

  echo'<script> window.location="../index.html"; </script> ';

  ?>
Blackboy
  • 21
  • 1
  • 3
  • 11

1 Answers1

2

try

<form action="" method="post" enctype="multipart/form-data">

 <h2>Your Contact Info</h2>
  <p>Your First Name* <br />
    <input type="text" name="firstName" id="firstName" required />
  </p>
  <p>Your Last Name* <br />
    <input type="text" name="lastName" id="lastName" required />
  </p>
  <p>Your Email* <br />
    <input type="email" name="email" id="email" required />
  </p>
  <p>Upload Your logo<br />
    <input type="file" name="uploaded_file" id="uploaded_file"> 
  </p>
  <input type="submit" name="submit" />
 </form>
<?php
if(isset($_POST['submit'])) {
 $emailAddress = 'ex@ex.com';
 require "class.phpmailer.php";
$msg = 'First Name:'.$_POST['firstName'].'<br /> Last name:'.$_POST['lastName'].'<br /> Email:'.$_POST['email'].'<br />';
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $_FILES["uploaded_file"]["name"]);
  $mail = new PHPMailer();
  $mail->IsMail();

  $mail->AddReplyTo($_POST['email'], $_POST['name']);
  $mail->AddAddress($emailAddress);
  $mail->SetFrom($_POST['email'], $_POST['name']);
  $mail->Subject = "Subject";
  $mail->MsgHTML($msg);
  $mail->AddAttachment( $_FILES["uploaded_file"]["name"]);
  $mail->Send();

  echo'<script> window.location="../index.html"; </script> ';
}
  ?>

For more info :- Send attachments with PHP Mail()?

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44