PHPMailer attachemnt不工作

问题描述:

我在phpmailer函数中添加了上传文件功能,当我想发送文件时,它只显示文件名,实际文件在附件中丢失。PHPMailer attachemnt不工作

有没有解决这个问题的想法。任何帮助将不胜感激。

寻找帮助

<?php 
 

 
if(isset($_POST['submit'])) 
 
{ 
 

 
$message= 
 
'Full Name: \t '.$_POST['fullname'].'<br /> 
 
Subject: \t '.$_POST['subject'].'<br /> 
 
Phone: \t '.$_POST['phone'].'<br /> 
 
Email: \t '.$_POST['emailid'].'<br /> 
 
Attachment: \t '.$_POST['attachment'].'<br /> 
 
Comments: \t '.$_POST['comments'].' 
 
'; 
 

 

 
require 'PHPMailer/PHPMailerAutoload.php'; 
 

 

 

 

 

 

 
$mail = new PHPMailer; 
 

 
//$mail->SMTPDebug = 3;        // Enable verbose debug output 
 

 
$mail->isSMTP();          // Set mailer to use SMTP 
 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
 
$mail->Username = '[email protected]';     // SMTP username 
 
$mail->Password = '******';       // SMTP password 
 
$mail->SMTPSecure = 'ssl';       // Enable TLS encryption, `ssl` also accepted 
 
$mail->Port = 465;         // TCP port to connect to 
 

 

 

 

 
$mail->SetFrom($_POST['emailid'], $_POST['fullname']); 
 
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']); 
 
$mail->Subject = "New Contact Form Enquiry";  // Subject (which isn't required) 
 
$mail->MsgHTML($message); 
 

 
$mail->addAttachment('attachment');   // Add attachments 
 
$mail->isHTML(true); 
 
    
 
    
 
    
 

 
\t $mail->addAddress('[email protected]', 'Website');  // Add a recipient 
 
    $result = $mail->Send(); \t \t // Send! 
 
\t $message = $result ? 'Successfully Sent!' : 'Sending Failed!';  
 
\t unset($mail); 
 

 

 
} 
 
?> 
 

 

 

 
    
 
<html> 
 
<head> 
 
    <title>Contact Form</title> 
 
</head> 
 
<body> 
 
\t \t \t \t \t 
 
\t \t <div style="margin: 100px auto 0;width: 300px;"> 
 
\t \t \t <h3>Contact Form</h3> 
 
\t \t \t <form name="form1" id="form1" action="" method="post"> 
 
\t \t \t \t \t <fieldset> 
 
\t \t \t \t \t <input type="text" name="fullname" placeholder="Full Name" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="subject" placeholder="Subject" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="phone" placeholder="Phone" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="emailid" placeholder="Email" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t <input type="file" name="attachment" id="attachment" > 
 
\t \t \t \t \t <br /> \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t <textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="submit" name="submit" value="Send" /> 
 
\t \t \t \t \t </fieldset> 
 
\t \t \t </form> 
 
\t \t \t <p><?php if(!empty($message)) echo $message; ?></p> 
 
\t \t </div> 
 
\t \t \t \t \t 
 
</body> 
 
</html>

+0

你要在这里使用的文件上传功能,不需要在任何目录下上传刚刚获得文件的临时路径并使用它作为附件,像这样$ attachment = $ _FILES ['attachment'] ['tmp_name'];并在您的表单标记 – sunilwananje

+0

中添加enctype =“multipart/form-data”请参阅下面的回答 – sunilwananje

+0

sir此代码正在使用邮件不会发送发送失败!信息会来 –

<?php 
 

 
if(isset($_POST['submit'])) 
 
{ 
 
$attachment = $_FILES['attachment']['tmp_name']; 
 
$attachment_name = $_FILES['attachment']['name']; 
 
$message= 
 
'Full Name: \t '.$_POST['fullname'].'<br /> 
 
Subject: \t '.$_POST['subject'].'<br /> 
 
Phone: \t '.$_POST['phone'].'<br /> 
 
Email: \t '.$_POST['emailid'].'<br /> 
 
Attachment: \t '.$attachment.'<br /> 
 
Comments: \t '.$_POST['comments'].' 
 
'; 
 

 

 
require 'PHPMailer/PHPMailerAutoload.php'; 
 

 

 

 

 

 

 
$mail = new PHPMailer; 
 

 
//$mail->SMTPDebug = 3;        // Enable verbose debug output 
 

 
$mail->isSMTP();          // Set mailer to use SMTP 
 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
 
$mail->Username = '[email protected]';     // SMTP username 
 
$mail->Password = '******';       // SMTP password 
 
$mail->SMTPSecure = 'ssl';       // Enable TLS encryption, `ssl` also accepted 
 
$mail->Port = 465;         // TCP port to connect to 
 

 

 

 

 
$mail->SetFrom($_POST['emailid'], $_POST['fullname']); 
 
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']); 
 
$mail->Subject = "New Contact Form Enquiry";  // Subject (which isn't required) 
 
$mail->MsgHTML($message); 
 

 
$mail->addAttachment($attachment,$attachment_name);   // Add attachments 
 
$mail->isHTML(true); 
 
    
 
    
 
    
 

 
\t $mail->addAddress('[email protected]', 'Website');  // Add a recipient 
 
    $result = $mail->Send(); \t \t // Send! 
 
\t $message = $result ? 'Successfully Sent!' : 'Sending Failed!';  
 
\t unset($mail); 
 

 

 
} 
 
?> 
 

 

 

 
    
 
<html> 
 
<head> 
 
    <title>Contact Form</title> 
 
</head> 
 
<body> 
 
\t \t \t \t \t 
 
\t \t <div style="margin: 100px auto 0;width: 300px;"> 
 
\t \t \t <h3>Contact Form</h3> 
 
\t \t \t <form name="form1" id="form1" action="" method="post" enctype="multipart/form-data"> 
 
\t \t \t \t \t <fieldset> 
 
\t \t \t \t \t <input type="text" name="fullname" placeholder="Full Name" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="subject" placeholder="Subject" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="phone" placeholder="Phone" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="text" name="emailid" placeholder="Email" /> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t <input type="file" name="attachment" id="attachment" > 
 
\t \t \t \t \t <br /> \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t <textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> 
 
\t \t \t \t \t <br /> 
 
\t \t \t \t \t <input type="submit" name="submit" value="Send" /> 
 
\t \t \t \t \t </fieldset> 
 
\t \t \t </form> 
 
\t \t \t <p><?php if(!empty($message)) echo $message; ?></p> 
 
\t \t </div> 
 
\t \t \t \t \t 
 
</body> 
 
</html>

+0

谢谢先生100%工作你是非常好的人 –