发送图像附件

时间:2013-05-19 12:05:07

标签: php ajax email

我有一个PHP表单,我正在尝试从它作为附件发送图像。我已在代码中修改了一些内容。

  1. 我不确定它是否会发送图像(因为我遇到了一些问题)
  2. 问题是当我在服务器上打开index.php甚至按钮
  3. 时,页面中都没有显示任何内容(当我运行下面的脚本时)

    以下是代码:

    <?php
    
    include_once("functions.php");
    // Process
    $action = isset($_POST["action"]) ? $_POST["action"] : "";
    if (empty($action)) 
    {
        // Send back the contact form HTML
        $output = "<form action='#' style='display:none'>
                   <input type='file' id='image' name='image' maxlength=50>";
    }
    
    require("class.phpmailer.php");
    $Email_to = "someone@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();
        }
    }//end of upload func'
    
    
    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(false);// send as HTML
    }
    
    
    ?>
    

    提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题1:

尝试使用该类发送邮件,而不添加任何变量。因此,只需制作一些程序代码,将邮件发送到您自己的地址,看看它是否到达。

对于问题2:

您正在将一些文字放入$output,但您永远不会输出$output