I have a script for sending emails with PHPmailer (the class is different because I use a CRUD but the functions are the same)
This is the script:
if (array_key_exists('recipient', $_POST)) {
$attachment = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name']));
move_uploaded_file($_FILES['attachment']['tmp_name'], $attachment);
$recipient = $_POST['recipient'];
$subject = $_POST['subject'];
$content = $_POST['content'];
//$attachment = $_POST['attachment'];
$email->Sender = "noreply@fw.net";
$email->Recipient = "noreply@fw.net";
if (!empty($_POST['recipient'])) {
$email->Recipient = $recipient;
} else {
foreach ($bcc as $bcc) {
$email->addBcc($bcc);
}
}
//$email->addRecipient = $bcc;
$email->Subject = $subject;
$email->Content = $content;
$email->addAttachment($attachment, 'MyFile');
$email->Format = "html";
//$email->send();
if(!$email->send()) {
$msg = '<div style="Color:red">Sent:</div>'. $email->SendErrDescription;
} else {
$msg = '<div style="Color:green">not Sent</div>';
header("Refresh:5");
}
}
this the form: email.php (work on the same page script / form)
<form id="mess" action="email.php" method="POST" enctype="multipart/form-data">
<?php if ($Page->CheckToken) { ?>
<input type="hidden" name="<?php echo TOKEN_NAME ?>" value="<?php echo $Page->Token ?>">
<?php } ?>
<div id="jsInfo" data-count="<?php echo $count; ?>"></div>
<div class="card card-primary card-outline">
.......
<div class="form-group">
<div class="btn btn-default btn-file">
<i class="fa fa-paperclip"></i> Allegato
<input type="hidden" name="MAX_FILE_SIZE" value="100000"> <input type="file" name="attachment" id="attachment">
</div>
<p class="help-block">Max. 10MB</p>
</div>
........
<button type="submit" value="Upload" name="submit" class="btn btn-primary"><i class="fa fa-envelope-o"></i> Send </button>
I changed the destination to php.ini I use wamp in local for testing from:
sys_temp_dir = "/tmp"
to
sys_temp_dir = "c:/Users/lt/Documents/Temp"
Question:
Why is not attached to the original file(pdf,jpg,bmp,txt ecc..), but the *.tmp file is attached. ? where am I wrong?
let's say I solved that.
don't works
$attachment = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name']));
move_uploaded_file($_FILES['attachment']['tmp_name'], $attachment);
works
$uploaddir = 'C:/wamp64/www/fw3/';
$attachment_tmp = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
move_uploaded_file($attachment_tmp, $uploaddir . $attachment_name);
I have two problems again ... 1) How to send the attachment to all recipients? 2) When sending a 4Mb file the page loads a lot of time, how can I modify the script to make the sending work in the background?