0
  1. 我正在使用此代码通过表单上传和附加图像,并将其发送到邮件(作为 html 而不是作为 smtp)。一旦我在网络上输入这个(代码)页面,我可以看到输出就好了,我可以单击“选择文件”并从某个目录中选择一个文件。但是,当进入页面时,我也可以立即看到回显“无效文件”,这可能是因为我还没有上传图像,但是代码运行并且没有等待我选择一些东西。我是否错过了一些将提交我的选择而不是发送的触发器?

  2. 当将其配置为像我一样作为 html 发送时,$mail->Send() 是否足够,当代码到达该命令时,将发送带有附件的邮件?或者我需要其他一些触发器来发送它?

谢谢你,

<?php
include_once("functions.php");
// Process
$action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action)) 
{
    $output = "<form action='#'>
               <h1>header: </h1>
                <label for='image'>upload:  </label>
                <input type='file' id='image' name='image' maxlength=50 >";

}
echo $output;
$image = $_POST["image"];
uploadImage($image);
require("class.phpmailer.php");
$Email_to = "some@gmail.com"; // the one that recieves the email
$email_from = "someone@someone.net";
$dir = "uploads/$filename";
chmod("uploads",0777);

function uploadImage($image){
    if ((($_FILES["image"]["type"] == "image/gif")
    || ($_FILES["image"]["type"] == "image/jpeg")
    || ($_FILES["image"]["type"] == "image/pjpeg")
    || ($_FILES["image"]["type"] == "image/jpg")
    || ($_FILES["image"]["type"] == "image/png"))
    && ($_FILES["image"]["size"] < 2097152)
    && (strlen($_FILES["image"]["name"]) < 51)){
        if ($_FILES["image"]["error"] > 0){
                echo "Return Code: " . $_FILES["image"]["error"];
            }
        else{
                echo "Upload: " . $_FILES["image"]["name"] . "<br />";
                echo "Type: " . $_FILES["image"]["type"] . "<br />";
                echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br />";

                if (file_exists("images/" . $_FILES["image"]["name"])){
                echo $_FILES["image"]["name"] . " already exists. ";
                }
                else{
                move_uploaded_file($_FILES["image"]["tmp_name"],
                        "images/" . $_FILES["image"]["name"]);
            }
            }
    }else{
        echo "Invalid file";
    }
    $filename = $_FILES["image"]["type"];
    $dir = "uploads/$filename";
    chmod("uploads",0777);
    $success = copy($_FILES[images][tmp_name], $dir);
    if ($success) {
    echo " Files Uploaded Successfully<BR>";
    SendIt();
    }
}

function SendIt() {
//
global $attachments,$Email_to,$Email_msg,$email_subject,$email_from;
//
$mail = new PHPMailer();
//$mail->IsSMTP();// send via SMTP
//$mail->Host = "localhost"; // SMTP servers
//$mail->SMTPAuth = false; // turn on/off SMTP authentication
$mail->From = $email_from;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;// set word wrap
//now Attach all files submitted
$mail->AddAttachment("uploads"."/".$_FILES["image"]["type"]);
//
$mail->IsHTML(true);// send as HTML

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

1 回答 1

0

你应该只在有 $action 时触发 uploadImage 函数:

...previous code...
if (empty($action)) 
{
?>
<form action=''>
    <h1>header: </h1>
    <label for='image'>upload:  </label>
    <input type='file' id='image' name='image' maxlength=50 >
</form>
<?php
    exit; // stop the upload script running
}

$image = $_POST["image"];
uploadImage($image);
require("class.phpmailer.php");
... rest of the code ...
于 2013-05-19T21:54:20.900 回答