无法使用PHPmailer发送附件

时间:2015-07-27 06:39:18

标签: php html email

我无法通过邮件发送附件,只有文本内容成功发送。

以下是我尝试发送邮件的表单。

<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>

email.php就在这里:

<?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('dinesh@xyz.com');

$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>

我几乎所有的东西都可以,但是我很瘦,但仍然缺少一些步骤。请在这方面帮助我。

2 个答案:

答案 0 :(得分:1)

上传后使用此代码(我认为这应该有效):

    <!DOCTYPE html>
    <html>

    <head>
        <title></title>
    </head>

    <body>
        <template repeat="{{ items }}">
            <a href="{{ Link }}">
                <img src="{{ Image }}" />
            </a>
        </template>
        <form>
            <legend>Enter new details:
                <legend>
                    <input type="text" name="Link" value="http://www.polymer-project.org" />
                    <br />
                    <input type="text" name="Image" value="http://www.polymer-project.org/images/logos/lockup.svg" />
                    <br />
                    <input type="submit" value="Add" />
        </form>

        <script type="text/javascript">
        var template = document.querySelector('template');
        template.model = {
            items: [{
                Link: 'http://falafel.com',
                Image: 'http://www.falafel.com/images/default-album/falafellogo.png'
            }]
        };

        var btnAdd = document.querySelector('[type="submit"]');
        btnAdd.addEventListener('click', function(e) {
            e.preventDefault();

            //BIND FORM INPUTS AS NEW ITEM
            template.model.items.push({
                Link: document.querySelector('[name="Link"]').value,
                Image: document.querySelector('[name="Image"]').value,
            });

            //CLEAR FORM FOR NEXT ENTRY
            document.querySelector('form').reset();
        });
        </script>

    </body>
    <!--script src="https://togetherjs.com/togetherjs.js"></script-->

    </html>

答案 1 :(得分:0)

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

可能重复 Send File Attachment from Form Using phpMailer and PHP