2014-03-02 52 views
-1

我试图用phpMailer脚本发送附件。附件不会与phpmailer一起发送

由于我的电子邮件发送正确,一切工作正常,但是我确实遇到了未发送附件的问题。

HTML部分:

<p> 
    <label>Attachment :</label> 
    <input name="doc" type="file"> 
</p> 

PHP:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$mail->isSMTP();    
$mail->Host = '*****'; 
$mail->SMTPAuth = true; 
$mail->Port = 465; 
$mail->Username = '*****'; 
$mail->Password = '*****'; 
$mail->SMTPSecure = 'ssl'; 

$mail->From = $_POST["name"]; 
$mail->FromName = 'Your Name'; 
$mail->Subject = 'Message Subject'; 
$mail->addAddress('*****');  

$mail->addAttachment($_FILES["doc"]); 
$mail->isHTML(true); 

$mail->Subject = 'Here is the subject'; 
$mail->Body = "You got a new message from your website : 

    Name: $_POST[name] 
    Company: $_POST[company] 
    Phone: $_POST[phone] 
    Email: $_POST[email] 
    Message: $_POST[message]"; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 
} 

?> 

<!DOCTYPE HTML> 
<html> 
<head> 
<title>Thanks!</title> 
</head> 
<body> 
<p> <img src="img/correct.png" alt="icon" style=" margin-right: 10px;">Thank you! We will get back to you soon.</p> 
</body> 
</html> 
+0

我将不胜感激,如果你能解释一下你的“反对票” –

回答

1
Try: 

    if (isset($_FILES['doc']) && 
     $_FILES['doc']['error'] == UPLOAD_ERR_OK) { 
     $mail->AddAttachment($_FILES['doc']['tmp_name'], 
          $_FILES['doc']['name']); 
    } 

Basic example can also be found [here](https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/AdvancedMail). 

The function definition for `AddAttachment` is: 

    public function AddAttachment($path, 
            $name = '', 
            $encoding = 'base64', 
            $type = 'application/octet-stream') 
0

请看看如何PHP file uploads工作。此阵列关键字:

$_FILES["doc"] 

...将永远不会包含文件。最多只能包含一个数组,其中包含有关在何处查找文件的信息。

+0

你可以帮我用一个例子或解决方案?问题是将此与phpmailer –

+0

[示例#2验证文件上载](http://es1.php.net/manual/en/features.file-upload.post-method.php#example-357)集成。我无法理解,如果将完整的数据阵列视为文件内容,您的上传如何在PhpMailer旁边工作。 –

+0

你有文件上传在你的应用程序的另一部分工作吗? –

0

未经检验的解决方案,但应该工作:

变化

$mail->addAttachment($_FILES["doc"]); 

$mail->addAttachment($_FILES["doc"]["tmp_name"], $_FILES["doc"]["name"], "base64", $_FILES["doc"]["tmp_type"]); 

但是,请考虑learning php upload file handling如已被别人评论。 请勿在未经验证的情况下使用用户输入。决不!