我想向您展示我的自定义函数与PHPmailer函数一起使用,我使用它来发送带有(总是)来自三个输入的三个附件的电子邮件。
我想问你,是否可以将这种代码的语气升级得更好?我是前端,PHP对我来说是一个新的巨大挑战。
附言。我希望这篇文章能帮助某人,为了自定义,您需要将$filePath
变量粘贴到带有输入saveFile
函数和数组中。
整个函数创建randomHash并将其$_FILES[input-name][name]
,然后将该文件移动到新的./uploads目录。将带有 [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();
?>
我会使用input_filter
函数而不是$_REQUEST
- 更安全的做法。它看起来像这样: input_filer(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)
您可以在此处找到有关该主题的更多信息:http://www.sitepoint.com/input-validation-using-filter-functions
在$mail->AltBody
中,您可能希望添加一些PHP_EOL
以将其分成几行。
您还可以考虑使用 sprintf
来实现更具可读性的代码:
sprints('
<h3>Nakaz Windykacyjny<h3><br />
Wierzyciel: %s<br />
Email: %s<br />Dłużnik: %s<br />
Kwota: %d',
$borrowman,
$email,
$lendman
)