Dinesh Suthar
Dinesh Suthar

Reputation: 89

Unable to send attachment using PHPmailer

I am unable to send attachment with the mail only textual contents are successfully sent.

Here is the form through which I am trying to send mail.

<form action="email.php" method="post" enctype="multipart/form-data">
            <table id="tab_form">
                <tr>
                    <td width="50%">
                        <input type="text" class="popInput" name="fname" placeholder="First Name"/>
                    </td>

                    <td width="50%">
                        <input type="text" class="popInput" name="lname" placeholder="Last Name"/>
                    </td>
                </tr>

                <tr>
                    <td width="50%">
                        <input type="text" class="popInput" name="LLno" placeholder="Contact Landline Number"/>
                    </td>

                    <td width="50%">
                        <input type="text" class="popInput" name="Mno" placeholder="Contact Mobile Number"/>
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <input type="email" class="popInput" name="Email" placeholder="Email Address"/>
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <input type="text" class="popInput" name="position" placeholder="Position Applied for"/>
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <textarea name="additional" placeholder="Additional Comments"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="file" name="file" class="popInput"/>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <input type="submit" name="Submit"  />
                    </td>
                    <td align="center">
                        <input type="button" value="Reset" />
                    </td>
                </tr>
            </table>
         </form>

And the email.php is here:

<?php
include("class.phpmailer.php");
$email=new PHPMailer();


$email->From=$_POST["Email"];
$email->FromName=$_POST["fname"]." ".$_POST["lname"];
$email->Subject="Application for ".$_POST["position"];

$body=$_POST["fname"]." ".$_POST["lname"]."\n";
$body.=$_POST["LLno"]."\n".$_POST["Mno"]."\n";
$body.="Additional Comments: ".$_POST["additional"];
$email->Body=$body;
$email->addAddress('[email protected]');

$uploaddir="uploads/";
$uploadfile=$uploaddir.basename($_FILES['file']['name']);
echo $uploadfile;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
}
else
{
    //echo "file not uploaded";
}
$file_to_attach="/".$uploaddir.$_FILES['file']['name'];
$file=$_FILES['file']['name'];
$email->addAttachment($file_to_attach,$file);
return $email->send();
?>
<script language="javascript">
    alert("Email Send Sucessfully");
    window.location = "index.php";
</script>

I have almost everything that i could but I thinl there is still some steps missing.Please help me in this regard.

Upvotes: 1

Views: 197

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

try

$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);

And Possible Duplicate of Send File Attachment from Form Using phpMailer and PHP

Upvotes: 0

Vikas Umrao
Vikas Umrao

Reputation: 2615

Use this code after uploading (I think this should work):

$file_to_attach="".$uploaddir.$_FILES['file']['name'];
$email->addAttachment($file_to_attach);
$email->send();

Upvotes: 1

Related Questions