I have a form where text fields must have an option to upload multiple files at once and then send them all via Email. My code is working when attaching only one file, I get everything right. When I attach several, I also receive the email, but only one file is attached. Can someone help me? To attach all selected files to Email.
Index:
<form action="MulAttachMail.php" method="post" enctype="multipart/form-data">
<h3>De: </h3>
<label>Seu Nome (opcional): </label> <input type="text" name="" class="" placeholder="Digite seu Nome"/>
<label>Seu Email: </label> <input type="email" name="sen_email" class="sen_email" placeholder="Digite seu Email"/>
<h3>Para: </h3>
<label>Email do Destinatário: </label> <input type="email" name="rec_email" class="rec_email" placeholder="Email da pessoa que vai receber o contato"/>
<label>Assunto: </label>
<input type="text" name="email_sub" class="" placeholder="Assunto da mensagem"/>
<label>Mensagem: </label>
<textarea name="box_msg" rows="10" cols="30"></textarea>
<div class="input_fields_wrap">
<div><input type="file" name="attachment" value="Attach File" id="first_attach" multiple></div>
</div>
<a class="add_field_button">Adicionar Mais Arquivos</a>
<input type="submit" value="Enviar" id="submit"/>
</form>
MulAttachMail.php - which sends and receives information -
<?php
// Include PHPMailerAutoload.php library file
include ("lib/PHPMailerAutoload.php");
$sen_name = "";
$sen_email = "";
$rec_email = "";
$email_sub = "";
$box_msg = "";
// Retrieving & storing user's submitted information
if (isset($_POST['sen_name'])) {
$sen_name = $_POST['sen_name'];
}
if (isset($_POST['sen_email'])) {
$sen_email = $_POST['sen_email'];
}
if (isset($_POST['rec_email'])) {
$rec_email = $_POST['rec_email'];
}
if (isset($_POST['email_sub'])) {
$email_sub = $_POST['email_sub'];
}
if (isset($_POST['box_msg'])) {
$box_msg = $_POST['box_msg'];
}
if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
// Define allowed extensions
$allowedExtentsoins = array('pdf', 'doc', 'docx', 'gif', 'jpeg', 'jpg', 'png', 'rtf', 'txt','zip');
foreach ($_FILES as $name => $file) {
if (!$file['name'] == "") {
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
$path_part = pathinfo($file_name);
$ext = $path_part['extension'];
// Checking for extension of attached files
if (!in_array($ext, $allowedExtentsoins)) {
echo "<script>alert('Sorry!!! ." . $ext ."Extension is not allowed!!! Try Again.')</script>";
$ext_error = FALSE;
}else{
$ext_error = True;
}
// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
move_uploaded_file($temp_name, $server_file);
array_push($files, $server_file);
}
}
if($ext_error != FALSE){
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tsl"; //tls
$mail->CharSet = 'utf-8';
$mail->Host = "mail.hostname.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "senhadoemail";
// Sender Email address
$mail->From = $sen_email;
// Sender name
$mail->FromName = $sen_name;
// Receiver Email address
$mail->addAddress($rec_email);
// Attaching files in the mail
foreach ($files as $file) {
$mail->addAttachment($file);
}
$mail->Subject = $email_sub;
$mail->Body = $box_msg;
$mail->WordWrap = 50;
// Sending message and checking status
if (!$mail->send()) {
echo "<script>alert('Sorry!!! Message was not sent. Mailer error: " . $mail->ErrorInfo . ")</script>";
exit;
} else {
echo "<script>alert('Congratulations!!! Your Email has been sent successfully!!!')</script>";
}
// Deleting files from the uploads folder
foreach ($files as $file) {
unlink($file);
}
echo "<script>window.location='index.php';</script>";
}else{
foreach ($files as $file) {
unlink($file);
}
echo "<script>window.location='index.php';</script>";
}
}
?>