2017-06-21 53 views
1

我在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此代碼正在使用郵件不會發送發送失敗!信息會來 –

回答

1

<?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%工作你是非常好的人 –