我正在尝试创建一个发送包含用户填充数据的电子邮件的表单。到目前为止,一切都正确发送,但附件从未包含在发送的电子邮件中。任何帮助,将不胜感激。
HTML表单询问用户数据。 “Mailerindex.html”
<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="file" placeholder="attachment" />
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>
PHP脚本。 “PHPMailer.php”
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.xxxxxxxx.edu';
$mail->SMTPAuth = false;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'TLS';
$mail->Port = 25;
$mail->setFrom('xxxxxxxx@gmail.com', 'Mailer');
$mail->addAddress('xxxxxxxxx@vt.edu', 'Joe User');
$mail->isHTML(true);
$fnew =$_POST["fname"];
$phone =$_POST["phoneNum"];
$newEmail =$_POST["email"];
$newDesc =$_POST["desc"];
$file_to_attach = $_FILES['file']['tmp_name'];
$filename=$_FILES['file']['name'];
$mail->AddAttachment( $file_to_attach , $filename );
$message = 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';
}