【问题标题】:Send image attachment using phpmailer使用 phpmailer 发送图片附件
【发布时间】:2013-05-19 17:49:28
【问题描述】:
  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;
}
}
?>      

【问题讨论】:

    标签: php html phpmailer


    【解决方案1】:

    只有在有 $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 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多