我似乎看不懂下面的内容:
- 如何使附加文件仅为特定文件(例如:only)可以附上.jpg文件)。
- 限制文件大小
文件发送后,从服务器上删除
<div class="white-jumbotron"> <div class="container"> <form action="" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">First Name</label> <div class="col-sm-10"> <input class="form-control" type="text" name="firstName" id="firstName" required /> <p class='text-danger'></p> </div> </div> <div class="form-group"> <label for="name" class="col-sm-2 control-label">Last Name</label> <div class="col-sm-10"> <input class="form-control" type="text" name="lastName" id="lastName" required /> <p class='text-danger'></p> </div> </div> <div class="form-group"> <label for="name" class="col-sm-2 control-label">Email</label> <div class="col-sm-10"> <input class="form-control" type="text" name="email" id="email" required /> <p class='text-danger'></p> </div> </div> <div class="form-group"> <label for="name" class="col-sm-2 control-label">Resume upload</label> <div class="col-sm-10"> <input type="file" name="uploaded_file" id="uploaded_file"> <br /> </div> </div> <div class="col-sm-10"> <input type="submit" name="submit" class="btn btn-green" /> </div> </form> <?php if(isset($_POST['submit'])) { $emailAddress = 'myemail@gmail.com'; require "class.phpmailer.php"; $msg = 'First Name:'.$_POST['firstName'].'<br /> Last name:'.$_POST['lastName'].'<br /> Email:'.$_POST['email'].'<br />'; move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $_FILES["uploaded_file"]["name"]); $mail = new PHPMailer(); $mail->IsMail(); $mail->AddReplyTo($_POST['email'], $_POST['name']); $mail->AddAddress($emailAddress); $mail->SetFrom($_POST['email'], $_POST['name']); $mail->Subject = "Subject"; $mail->MsgHTML($msg); $mail->AddAttachment( $_FILES["uploaded_file"]["name"]); $mail->Send(); echo'<script> window.location="../careers-sent.php"; </script> '; } ?> </div> </div>
首先,将视图(
1)在处理中,在文件名
上加一个条件if (preg_match("/\.jpg$/",$_FILES["uploaded_file"]["name"]))
{
//OK
}
else
{
//KO, redirect to error page
}
2)不能在这里测试,但我想也可以有一个"大小"的文件属性
3)检查发送
if ($mail->send())
{
unlink($_FILES["uploaded_file"]["name"]);
}
else
{
//KO, log to debug file
}
编辑:代码集成
view.php (rename at方便)
<div class="white-jumbotron">
<div class="container">
<form action="" method="post" action="val_mail.php" enctype="multipart/form-data">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="firstName" id="firstName" required />
<p class='text-danger'></p>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="lastName" id="lastName" required />
<p class='text-danger'></p>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="email" id="email" required />
<p class='text-danger'></p>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Resume upload</label>
<div class="col-sm-10">
<input type="file" name="uploaded_file" id="uploaded_file">
<br />
</div>
</div>
<div class="col-sm-10">
<input type="submit" name="submit" class="btn btn-green" />
</div>
</form>
val_mail.php(重命名方便,但与上面的表单的action
属性匹配)
<?php
if(isset($_POST['submit'])) {
if (!preg_match("/\.jpg$/",$_FILES["uploaded_file"]["name"]))
{
echo'<script> window.location="../error-attachment.php"; </script> '; exit;// create error page
}
$emailAddress = 'myemail@gmail.com';
require "class.phpmailer.php";
$msg = 'First Name:'.$_POST['firstName'].'<br /> Last name:'.$_POST['lastName'].'<br /> Email:'.$_POST['email'].'<br />';
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $_FILES["uploaded_file"]["name"]);
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Subject";
$mail->MsgHTML($msg);
$mail->AddAttachment( $_FILES["uploaded_file"]["name"]);
if ($mail->Send())
{
unlink($_FILES["uploaded_file"]["name"]);
}
else
{
echo'<script> window.location="../error-sent.php"; </script> '; exit;// create error page
}
echo'<script> window.location="../careers-sent.php"; </script> ';
}
?>
Try This,
if (isset($_FILES['uploaded_file']) &&
$_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
示例:https://code.google.com/archive/a/apache-extras.org/p/phpmailer/wikis/AdvancedMail.wiki
相关内容
- 没有找到相关文章
最新更新
- Check for alt + key js
- 我如何检查一个文件名后缀对很多选项简洁?
- 尝试将整数转换为ACGT DNA序列
- 从具有相似前缀的列中划分元素
- 为什么我的一些网站内容没有加载?
- MySQL SELECT没有找到已知值
- 新的r,抓取列值时,知道另一个
- 使用Accord.Video.FFMPEG,我得到了"parameter is not valid exception"。我该如何解决?
- c -如何获得exec()的输出?
- r语言 - 我如何将我的数据帧从宽格式转换为长格式,在不同的时间记录变量?
- 如何在boost multi_array容器中存储值?
- 单独的页面vs更新同一页面上的组件
- 如何创建唯一索引,这需要一组属性是唯一的?
- 如何触发按钮与键盘和活动
- 验证中的问题
- 在java中用空格替换撇号
- 将请求发送到多环境公共包中的适当API
- r语言 - 当变量由几个部分组成时,将 tibble 转换为长格式
- 返回填充空值的每个月的最新值
- 如何在循环内外使用来自user的输入变量
- 如何通过从网站的前端发出 ajax 请求(在同一域上内置 react )来获取 wordpress 中的当前用户 ID
- 标记器更改词汇表条目
- python请求和concurrent.futures问题
- 删除新提要中不存在的旧行
- 如何在颤振中将可变大小的小部件剪辑为固定大小?
- 限制安排数量
- 在r的嵌套列表内计算
- JavaScript使用自定义布尔值对日期排序
- Quarkus K8S操作器-运行测试时NoSuchField异常
- 从抓取数据中重新格式化字符串以满足关键字参数
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium