这是我第一次尝试使用phpMailer而我没有收到任何附件。其他所有东西都被发送但附件。
uploads
目录位于附加文件所在的根目录之后。我的php表单有问题吗。
<?php
require('class.phpmailer.php');
//var_dump($_POST);
//die;
$subject = "Consignment Form";
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$target_dir = "/uploads/";
$target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
// put your email
$to = 'gslonina7@gmail.com';
$email_subject = "Consignment form submitted by: $name";
// create email body and send it
$email_body = "You have received a new message. \n\n".
"Here are the details:\n \nName: $name \n ".
"Email: $email_address\n \nPhone: $phone \n".
"Message: $message \n ";
$email = new PHPMailer();
$email->From = $email_address;
$email->FromName = $name;
$email->Subject = $email_subject;
$email->Body = $email_body;
$email->AddAddress( 'gslonina7@gmail.com' );
$email->AddAttachment( $target_file );
return $email->Send();
?>
<form role="form" action="mail_consignment_new.php" class="contact-form validation-engine ajax-send">
<div class="row">
<div class="col-sm-4 form-group">
<label class="sr-only" for="input_name">Name *</label>
<input type="text" name="name" class="form-control validate[required]" id="input_name" placeholder="Name *">
</div>
<div class="col-sm-4 form-group">
<label class="sr-only" for="input_email">Email *</label>
<input type="email" name="email" class="form-control validate[required,custom[email]]" id="input_email" placeholder="Email *">
</div>
<div class="col-sm-4 form-group">
<label class="sr-only" for="input_subject">Phone</label>
<input type="text" name="phone" class="form-control" id="input_phone" placeholder="Phone">
</div>
</div>
<div class="form-group">
<label class="sr-only" for="uploaded_file">Select A File To Upload:</label>
<input type="file" name="uploaded_file">
</div>
<div class="form-group">
<label class="sr-only" for="input_message">Message</label>
<textarea name="message" class="form-control validate[required]" rows="7" id="input_message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default btn-wide">Send</button>
<span class="loading-spinner" style="display:none;"></span>
</div>
</form>
答案 0 :(得分:0)
将您的代码基于PHPMailer提供的示例,尤其是the basic contact form example。看起来你已经在其他地方使用了一个过时的,并且有很多基本的错误。
从不使用用户提供的地址作为发件人地址。这是伪造的,并且会导致您的邮件无法发送,因为您将无法通过SPF检查。将您的地址作为发件人地址和提交者的地址作为回复地址。
您处理上传文件的方式也是错误的 - refer to the PHP manual。您再次信任用户输入,因此可能存在漏洞;您必须使用is_uploaded_file
或move_uploaded_file
检查上传的文件 - 名为send_file_upload的示例显示了如何安全地处理上传。
$target_dir = "/uploads/";
这不是相对路径 - 除非/uploads
确实存在于文件系统的根目录(这不太可能),否则您的路径将是错误的。将其构建为相对路径,或找出您从哪里开始,例如其中之一:
$target_dir = "./uploads/";
$target_dir = __DIR__ . "/uploads/";
最后,PHPMailer的方法通常会在成功时返回true,因此您可以检查是否为对addAttachment
的调用是否成功。