我为我的生命无法得到这个附件发送。我已经按照Git Hub上的教程进行了操作,但是继续获得“未定义的变量:邮件在第41行的/Applications/XAMPP/xamppfiles/htdocs/test/PHPMailer.php”,因为它根本没有达到第二个if语句。这是因为$ uploadfile是“bool(false)”(我运行了var转储)。基本上我的问题是为什么$ uploadfile不接收任何东西?任何帮助,将不胜感激。
HTML code:
<html>
<head>
<link rel ="stylesheet" type = "text/css" href = "style.css" />
</head>
<div class="form-style-8">
<body>
<form action= "PHPMailer.php" method ="POST" id="from" enctype="multipart/form-data">
<input type="text" name="fname" placeholder="Your Name"/>
<input type="number" name="phoneNum" placeholder="Phone Number"/>
<input type="email" name="email" placeholder="Email Address"/>
<input type="text" name="desc" placeholder="Leave Description"/>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="userfile" placeholder="attachment" />
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>
PHP代码:
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
var_dump($uploadfile);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.johnshopkins.edu';
$mail->SMTPAuth = false;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'TLS';
$mail->Port = 25;
$mail->setFrom('abrown5991@gmail.com', 'Mailer');
$mail->addAddress('andrewb7@vt.edu', 'Joe User');
$mail->isHTML(true);
$fnew =$_POST["fname"];
$phone =$_POST["phoneNum"];
$newEmail =$_POST["email"];
$newDesc =$_POST["desc"];
$mail->AddAttachment( $uploadfile, 'my file');
$message = $str = implode(' ', array($fnew,$phone,$newEmail,$newDesc));
$mail->Subject = 'Auto Email';
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}