我想告诉你,我的自定义函数使用PHPmailer函数,我用它来发送带有(总是)三个输入的三个附件的电子邮件。
我想问你,有可能升级这种代码的语气更好吗?我是前端,PHP对我来说是一个新的巨大挑战。
PS。我希望这篇文章可以帮助某些人,为了进行自定义,您需要将$filePath
变量粘贴到saveFile
函数和带有输入的数组中。
在将该文件移动到新的./uploads目录之后,整个函数创建randomHash并将其连接到$_FILES[input-name][name]
。将[name]的新补丁保存到全局数组$patch
。函数checkData
检查文件类型。之后,global $patch
为文件提供补丁,以便在sendMail()
函数中附加邮件附件。
<?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();
?>
答案 0 :(得分:0)
I would use input_filter
function instead of $_REQUEST
- much safer thing to do. It looks like this: input_filer(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)
More on the topic you can find here: http://www.sitepoint.com/input-validation-using-filter-functions
In $mail->AltBody
you probably want to add some PHP_EOL
to break it into lines.
You can also think about using sprintf
to achieve more readable code:
sprints('
<h3>Nakaz Windykacyjny<h3><br />
Wierzyciel: %s<br />
Email: %s<br />Dłużnik: %s<br />
Kwota: %d',
$borrowman,
$email,
$lendman
)