dongtiandexue123456 2016-12-14 15:23
浏览 26

Phpmailer Addattachment出错了

i'm a php beginner,and my english can be wrong... i tried to find a way to send a mail via phpmailer with an attachment for a form. At the moment the file is uploaded on a directory on the server, but there's 2 files, one is good, the other is 0 byte, and the code attach that file... the wrong one! and i can't find what's wrong... Also there's no message show when the mail is sent... I am very gratefull if someone can help me!

HTML:

<form action="formulaire.php" method="post" enctype="multipart/form-data">
    <table align="center"><tr><td><label for="nom">Votre nom :</label></td>
        <td><input type="text" name="nom" required/><br></td></tr>
        <tr><td><label for="prenom">Votre prénom: </label></td>
        <td><input type="text" name="prenom" required/><br></td></tr>
        <tr><td><label for="societe">Société: </label></td>
        <td><input type="text" name="societe" required/><br></td></tr>
        <tr><td><label for="phone">Téléphone: </label></td>
        <td><input type="text" name="phone" required/><br></td></tr>
        <tr><td><label for="email">Votre E-mail: </label></td>
    <td><input type="email" name="email" required/><br></td></tr>
    <tr><td><label for="message">Texte explicatif :</label></td><br>
        <td><textarea name="message" rows="2" cols="50" required></textarea></td></tr>
    <tr><td><input type="hidden" name="MAX_FILE_SIZE" value="10000000"> Send this file: <input name="userfile" type="file"></td></tr>
        <tr><td></td></tr>
        <tr><td align="center"><input type="submit" value="Envoyer"></td></tr></table>
</form>

PHP:

<?php
            if (array_key_exists('userfile', $_FILES)) {
            $uploadfile = tempnam('upload', $_POST['nom']);
            if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile.".jpg")) {
            require_once("PHPMailer/class.phpmailer.php");
            require_once('PHPMailer/PHPMailerAutoload.php');
            $mail = new PHPMailer();
            $mail->From = $_POST['email'];
            $mail->IsMail ();
            $mail->ClearAddresses ();
            $mail->AddAddress ("xxx@xxxxx.com");
            $mail->isHTML(true); // Set email format to HTML
            $mail->Subject = 'xxxxx';
            $mail->Body = '<ul>
            <li>Nom : '. $_POST['nom'] .'</li>
            <li>Prenom : '. $_POST['prenom'] .'</li>
            <li>Societe : '. $_POST['societe'] .'</li>
            <li>Telephone : '. $_POST['phone'] .'</li>
            <li>E-mail : '. $_POST['email'] .'</li>
            '.$filename;'
        </ul>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
            $mail->addAttachment($uploadfile, '');
            if(!$mail->send()) {
            $msg = "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg = "Message sent!";
        }
    } else {
        $msg = 'Failed to move file to ' . $uploadfile;
    }
}
?>
  • 写回答

1条回答 默认 最新

  • doupiai5597 2016-12-30 18:05
    关注

    tempnam() actually creates an empty file when you call it. This is how it "reserves" a unique temporary file for later use. However, when you call move_uploaded_file() you are appending ".jpg" to the end. This is why you have two copies of the files, and one them is empty.

    Later in your message, you are attaching $uploadfile instead of $uploadfile.'.jpg'; this is why the 0-length file is the one attached to your e-mail. Note that with the second paramater, you can name the file whatever you want and that will be used instead of the file's name on disk. This would be better in your case because the user won't get a file with a random gibberish name; instead you can use the original name from the $_FILES['userfile'] array.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。