phpMailer 中的附件
Attachments in phpMailer
我似乎无法理解以下内容:
- 如何让附件只是一个特定的文件(例如:只有
可以附加一个 .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) 不能在这里测试,但我想也可以有文件的 "size" 属性
3) 检查发送
if ($mail->send())
{
unlink($_FILES["uploaded_file"]["name"]);
}
else
{
//KO, log to debug file
}
编辑:代码集成
view.php(方便重命名)
<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> ';
}
?>
试试这个,
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
我似乎无法理解以下内容:
- 如何让附件只是一个特定的文件(例如:只有 可以附加一个 .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) 不能在这里测试,但我想也可以有文件的 "size" 属性
3) 检查发送
if ($mail->send())
{
unlink($_FILES["uploaded_file"]["name"]);
}
else
{
//KO, log to debug file
}
编辑:代码集成
view.php(方便重命名)
<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> ';
}
?>
试试这个,
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