I would like to show you, my custom functions working with PHPmailer function, which I use for sending email with (always) three attachments from three inputs.
I would like to ask you, is possible to upgrade this tone of code for better? I am front-end and PHP is a new great challenge to me.
PS. I hope this post will help someone, for customize, you need to paste your $filePath
variable in saveFile
function and arrays with inputs.
Whole function creates randomHash and concate it to $_FILES[input-name][name]
, after that file is moved to new ./uploads directory. Save new patch with [name] to global array $patch
. Function checkData
check filetypes. After that global $patch
serves patches for files to attach them like mail attachments in sendMail()
function.
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function checkData()
{
$typeArray = [ $_FILES['file_one']['type'], $_FILES['file_two']['type'], $_FILES['file_three']['type'] ];
foreach ($typeArray as $key) {
if ($key != 'image/jpeg' && $key != 'application/msword' && $key != 'image/png' && $key != 'application/pdf' && $key != 'application/zip')
{
echo "File is incorrect!";
return false;
}
else {
echo "File is correct!";
}
}
return true;
}
function saveFile()
{
if(checkData() == true)
{
global $patch;
$patch = array();
$multiArray =
[
[$_FILES['file_one']['name'], $_FILES['file_one']['tmp_name']],
[$_FILES['file_two']['name'], $_FILES['file_two']['tmp_name']],
[$_FILES['file_three']['name'], $_FILES['file_three']['tmp_name']]
];
foreach ($multiArray as $key)
{
echo "<br />Key: ".$key[0]."\n";
echo "Key_tmp: ".$key[1]."\n";
$randomString = generateRandomString();
$patchFile = './uploads/'.$randomString.$key[0];
echo "<br />Check patchFile: $patchFile";
if(is_uploaded_file($key[1]))
{
echo "<br />Begin uploading to directory...<br />";
if(!move_uploaded_file($key[1], $patchFile))
{
echo 'Trouble with copy file to uploads directory.';
return false;
}
else {
echo "File was saved in uploads directory";
}
}
else
{
echo "Uploading to directory... FAILED!";
return false;
}
array_push($patch, $patchFile);
}
var_dump($patch);
return true;
}
else
{
echo "File format was incorrect.";
return false;
}
}
function sendMail() {
if(saveFile() == true)
{
global $patch;
echo "Check $patch variable:<br />";
var_dump($patch);
require 'PHPMailerAutoload.php';
$email = $_REQUEST['email'];
$borrowman = $_REQUEST['borrowman'];
$lendman = $_REQUEST['lendman'];
$cash = $_REQUEST['cash'];
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'XXXXX'; // SMTP username
$mail->Password = 'XXXXX'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->setFrom($email, 'Zgłoszenie Nakaz');
$mail->addAddress('lechowski.g@gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('lechowski.g@gmail.com'); // Name is optional
$mail->addReplyTo('lechowski.g@gmail.com', 'Information');
$mail->addCC('lechowski.g@gmail.com');
$mail->addBCC('lechowski.g@gmail.com');
foreach ($patch as $attachment) {
$mail->addAttachment($attachment);
}
#$mail->addAttachment($patchFile); // Add attachments
#$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Zgłoszenie - Nakaz Windykacyjny';
$mail->Body = '<h3>Nakaz Windykacyjny<h3><br />Wierzyciel: '.$borrowman.'<br />Email: '.$email.'<br />Dłużnik: '.$lendman.'<br />Kwota: '.$cash;
$mail->AltBody = 'Nakaz Windykacyjny Wierzyciel: '.$borrowman.'Email: '.$email.'Dłużnik: '.$lendman.'Kwota: '.$cash;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
else
{
echo "Message was not sended, some problem with files was detected.";
}
}
sendMail();
?>