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;
}
}
?>
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.