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