I am creating a mailing-list service. I need to attach dynamically one or more files into an email, using php, phpmailer and only one "slot" for adding files in the html form. I just know how to send one attachment, not two or more, in the same email.
here u are the html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="main_container">
<form action="phpmailer/sendmail.php" method="post" enctype="multipart/form-data" id="prenota">
<div class="form_title">
<h2>Mailing List</h2>
</div>
<div class="form_title">
<p>Message:</p>
</div>
<label for="message">
<textarea name="message"></textarea>
</label>
<label for="file">
Select one or more file to send
<input name="file" type="file" id="file_ok">
</label>
<div class="submit-container">
<input class="submit-button" type="submit" name="submit" value="Invia"><br>
</div>
</form>
</div>
</body>
</html>
and the php for the phpmailer (sendmail.php, it works with the hosting service I am usign):
<?php
require_once('libs/PHPMailer/PHPMailerAutoload.php');
$db= new PDO('mysql:host=00.000.000.000;dbname=dbname_1', 'dbname', 'dbpass');//connection setup
$m_list = $db->query("
SELECT id, nome, email
FROM mailing_list
")->fetchAll(PDO::FETCH_ASSOC);
$mail = new PHPMailer(true);
$mail->Port = 25;
$mail->CharSet = 'UTF-8';
//variables
$email_receiver = $m_list['email'];
$name_receiver = $m_list['nome'];
$nome_sender= "mr x";
$messagge= stripslashes($_POST['message']);
foreach ($m_list as $eachmail) {
$email_receiver = $eachmail['email'];
$name_receiver = $eachmail['nome'];
$mail->From = "service@email.it";
$mail->FromName = "service";
$mail->AddReplyTo($email_receiver, $name_receiver);
$mail->MsgHTML($body);
$mail->AddAddress($email_receiver, "dynamic name");
$mail->Subject = "Message from website";
$body = '<strong>email send to:</strong> ' . $name_receiver . '<br/>
<strong>email:</strong> ' . $email_receiver . '<br/>
<strong>message:</strong> ' . stripslashes($message) . '<br/><br/>';
$mail->MsgHTML($body);
$mail->Send(true);
$mail->ClearAllRecipients(true);
}
echo "mail sent";
?>
I imagine I need to store the files ito an array and then, for each files, create the relative attachment. Unfortunately I can't. Any help?
First of all I used an "injection" of Jquery, in order to add as much as inputs file I want in the dom of the main form, and to delete them if desired.
$( document ).ready(function() {
$( '#add_more' ).click(function() {
var current_count = $('input[type="file"]').length;
var next_count = current_count +1;
$('#prenota').prepend('<p id="here_' + next_count + '"><input type="file" name="file_' + next_count + '" /></p>')
$('#here_' + next_count + '').append('<a class="delete_this" href="#">delete</a>');
});
$('.delete_this').live('click', function(){
var current_todelete = $(this).closest("p").attr("id");
$('#' + current_todelete + '').remove();
});
});
Then I added some strings in the php file designated to collect post information and sendig emails, making it capable to carry attachments (as Synchro suggested me):
if (isset($_FILES)){
foreach ($_FILES as $file) {
$filename = $file['name'];
$filetype = $file['type'];
$filetmp_name = $file['tmp_name'];
$filesize = $file['size'];
if($filename){
$temPath = '../../../../../public/' . basename($filename);
//var_dump($temPath);
//die();
if (move_uploaded_file($filetmp_name, $temPath)) {
$mail->AddAttachment($temPath, $filename);
}
}
}
}else{
$mail->AddAttachment(false);
}
Hoping to be helpful to people with the same problem of mine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.