是否可以通过电子邮件将文件从我的服务器发送给我的客户和我自己?

时间:2014-02-09 01:51:20

标签: javascript php email

有没有人知道一个开源脚本会将文件从我的服务器通过电子邮件发送到两个电子邮件地址?

我有一个使用svgedit的网站,当用户点击保存按钮时,它会将svg和png(具有唯一的文件名,如Ux324tvY.png)保存到我服务器上的文件夹中。然后当他们将该项目添加到Mal的购物车时。

我有一个脚本来获取该唯一文件名并将其添加为购物车中的项目。然后他们填写他们的联系电子邮件和信息,然后点击继续付款。

在此事件“onbeforeunload”之后,我想触发一个php脚本来获取他们输入的唯一文件名和客户电子邮件地址,并将png发送到我的电子邮件地址。

我想尽可能简单。

我找到了一个静态表单脚本,其中包含用户输入以上传本地文件。

是否可以在下面修改此脚本以实现目标,还是有更好的选择?

    <?php

// This will show in the browsers title bar and at the top of the form.
$websitename="MyWebsiteName"; 

// Allowed file types. Please remember to keep the format of this array, add the file extensions you want
// WITHOUT the dot. Please also be aware that certain file types (such as exe) may contain malware.
$allowtypes=array("pdf", "svg", "jpg", "png");

// What's your email address? Seperate email addresses with commas for multiple email addresses.
$myemail="me@myemail.com";

// What priority should the script send the mail? 1 (Highest), 2 (High), 3 (Normal), 4 (Low), 5 (Lowest).
$priority="3"; 

// Should we allow visitors to attach files? How Many? 0 = Do not allow attachments,
// 1 = allow only 1 file to be attached, 2 = allow two files etc.
$allowattach="1"; 

// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
$max_file_size="1024";

// Maximum file size for all attachments combined in KB. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
$max_file_total="2048";

// Value for the Submit Button
$submitvalue=" Sumit Your File "; 

// Value for the Reset Button
$resetvalue=" Reset Form ";

// Default subject? This will be sent if the user does not type in a subject
$defaultsubject="No Subject"; 

// Because many requested it, this feature will add a drop down box for the user to select a array of
// subjects that you specify below. 
// True = Use this feature, False = do not use this feature
$use_subject_drop=false;

// This is an array of the email subjects the user can pick from. Make sure you keep the format of
// this array or you will get errors.
// Look at <http://novahq.net/forum/showthread.php?t=38718> for examples on how to use this feature.
$subjects=array("Department 1", "Department 2", "Department 3");

// This is an array of the email addresses for the array above. There must be an email FOR EACH
// array value specified above. You can have only 1 department if you want.
// YOU MUST HAVE THE SAME AMMOUNT OF $subjects and $emails or this WILL NOT work correctly!
// The emails also must be in order for what you specify above!
// Seperate email addresses by a comma to send an email to multiple addresses.
$emails=array("dept_1@domain.com", "dept_2@domain.com", "dept_3@domain.com");

// This is the message that is sent after the email has been sent. You can use html here.
// If you want to redirect users to another page on your website use this:
// Correction: to redirect, the code is placed near the very end of this entire code!
// printf("<script>location.href='http://www.mahogan.com/calc-wpx5/cart.html'</script>");
// $thanksmessage="Thank you! Your email has been sent, we will respond shortly."; 

/*
//================================================================================
* ! ATTENTION !
//================================================================================
: Don't edit below this line.
*/

// Function to get the extension of the uploaded file.
function get_ext($key) { 
    $key=strtolower(substr(strrchr($key, "."), 1));
    $key=str_replace("jpeg", "jpg", $key);
    return $key;
}

// Function used to attach files to the message
function phattach($file, $name, $boundary) {

    $fp=fopen($file, "r");
    $str=fread($fp, filesize($file));
    $str=chunk_split(base64_encode($str));
    $message="--".$boundary."\n";
    $message.="Content-Type: application/octet-stream; name=\"".$name."\"\n";
    $message.="Content-disposition: attachment; filename=\"".$name."\"\n"; 
    $message.="Content-Transfer-Encoding: base64\n";
    $message.="\n";
    $message.="$str\n";
    $message.="\n";

    return $message;
}

//Little bit of security from people forging headers. People are mean sometimes :(
function clean_msg($key) {
    $key=str_replace("\r", "", $key);
    $key=str_replace("\n", "", $key);
    $find=array(
        "/bcc\:/i",
        "/Content\-Type\:/i",
        "/Mime\-Type\:/i",
        "/cc\:/i",
        "/to\:/i"
    );
  $key=preg_replace($find, "", $key);
  return $key;
}

// Initilize some variables
$error="";
$sent_mail=false;

// When the form is submitted
If($_POST['submit']==true) {
    extract($_POST, EXTR_SKIP);

        // Check the form for errors
        If(trim($yourname)=="") { 
            $error.="You did not enter your name!<br />";
        }

        If(trim($youremail)=="") { 
            $error.="You did not enter your email!<br />";
        } Elseif(!preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/", $youremail)) {
            $error.="Invalid email address.<br />";
        }

        If(trim($emailsubject)=="") {
            $emailsubject=$defaultsubject;
        }
/* Commented Out, since the message box has been also commented out!
        If(trim($yourmessage)=="") { 
            $error.="You did not enter a message!<br />";
        }
*/      
        // Verify Attchment info
        If($allowattach > 0) {

            // Get the total size of all uploaded files
            If((array_sum($_FILES['attachment']['size'])) > ($max_file_total*1024)) {

                $error.="The max size allowed for all your files is ".$max_file_total."kb<br />";

            } Else {

                //Loop through each of the files
                For($i=0; $i <= $allowattach-1; $i++) {

                    If($_FILES['attachment']['name'][$i]) {

                        //Check if the file type uploaded is a valid file type. 
                        If(!in_array(get_ext($_FILES['attachment']['name'][$i]), $allowtypes)) {

                            $error.= "Invalid file type for your file: ".$_FILES['attachment']['name'][$i]."<br />";

                        //Check the size of each file
                        } Elseif(($_FILES['attachment']['size'][$i]) > ($max_file_size*1024)) {

                            $error.= "Your file: ".$_FILES['attachment']['name'][$i]." is to big.<br />";

                        } // If in_array

                    } // If Files

                } // For

            } // Else array_sum($_FILES['attachment']['size'])

        } // If Allowattach

    If($error) {

        $display_message=$error;

    } Else {

        If($use_subject_drop AND is_array($subjects) AND is_array($emails)) {
            $subject_count=count($subjects);
            $email_count=count($emails);

            If($subject_count==$email_count) {

                $myemail=$emails[$emailsubject];
                $emailsubject=$subjects[$emailsubject];

            } // If $subject_count

        } // If $use_subject_drop

        $boundary=md5(uniqid(time()));

        //Headers
        $headers="Return-Path: <".clean_msg($youremail).">\n";
        $headers.="From: ".clean_msg($yourname)." <".clean_msg($youremail).">\n";
        $headers.="X-Mailer: PHP/".phpversion()."\n";
        $headers.="X-Sender: ".$_SERVER['REMOTE_ADDR']."\n";
        $headers.="X-Priority: ".$priority."\n"; 
        $headers.="MIME-Version: 1.0\n";
        $headers.="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
        $headers.="This is a multi-part message in MIME format.\n";

        //Message
        $message = "--".$boundary."\n";
        $message.="Content-Type: text/html; charset=\"iso-8859-1\"\n";
        $message.="Content-Transfer-Encoding: quoted-printable\n";
        $message.="\n";
        $message.=clean_msg(nl2br(strip_tags($yourmessage)));
        $message.="\n";

        //Add attachments to message
        If($allowattach > 0) {

            For($i=0; $i <= $allowattach-1; $i++) {

                If($_FILES['attachment']['tmp_name'][$i]) {

                    $message.=phattach($_FILES['attachment']['tmp_name'][$i], $_FILES['attachment']['name'][$i], $boundary);

                } //If $_FILES['attachment']['name'][$i]

            } //For

        } // If

        // End the message
        $message.="--".$boundary."--\n";

        // Send the completed message
        If(!mail($myemail, clean_msg($emailsubject), $message, $headers)) {

            Exit("An error has occured, please report this to the website administrator.\n");

        } Else {

            $sent_mail=true;

        }

    } // Else

} // $_POST

/*
//================================================================================
* Start the form layout
//================================================================================
:- Use the html below to customize the form.
*/

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php  echo $websitename;  ?> </title>

<style type="text/css">
    body{
        background-color:#646363;
        font-family: Verdana, Arial, sans-serif;
        font-size: 11pt;
        text-align:center;  
        color: #ffffff;
    }

    .error_message{
        font-family: Verdana, Arial, sans-serif;
        font-size: 11pt;
        color: #E56E02;
    }

    .thanks_message{
        font-family: Verdana, Arial, sans-serif;
        font-size: 11pt;
        color: #000000;
    }

    a:link{
        text-decoration:none;
        color: #000000;
    }
    a:visited{
        text-decoration:none;
        color: #000000;
    }
    a:hover{
        text-decoration:none;
        color: #000000;
    }

</style>

<script type="text/javascript">
var error="";
e_regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;

function Checkit(theform) {
    if(theform.yourname.value=="") {
        error+="You did not enter your name\n";
    }

    if(theform.youremail.value=="") {
        error+="You did not enter your email\n";
    } else if(!e_regex.test(theform.youremail.value)) {
        error+="Invalid email address\n";
    }


    if(error) {
        alert('**The form returned the following errors:**\n\n' + error);
        error="";
        return false;
    } else {
        return true;
    }
}
</script>

</head>
<body>
<?If($display_message) {?>

<div align="center" class="error_message"><b><?=$display_message;?></b></div>
<br />

<?}?>

<?If($sent_mail!=true) {?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" name="phmailer" onsubmit="return Checkit(this);">
<table align="center" class="table">
    <tr>
        <td colspan="2" class="table_header" width="100%"><? /* =$websitename; */?></td>
    </tr>
    <?If($allowattach > 0) {?>
        <tr>
            <td width="100%" class="attach_info" colspan="2">
                <!-- <b>Valid Attachment Types:</b> --><? /* =implode($allowtypes, ", "); */?><br />
                <!-- <b>Max size per file:</b> --><? /* =$max_file_size */ ?>  <!--kb.-->
                <!--<b>   Max combined file size:</b> --><? /* =$max_file_total */?><!--kb.-->
            </td>
        </tr>
    <?}?>

    <tr>
        <!-- <td width="30%" class="table_body">Your Name:</td> -->
        <td width="90%" class="table_body"><p><input name="yourname" type="text"  size="30" cellpadding="50" value="<?=stripslashes(htmlspecialchars($yourname));?>" /><span class="error_message">*</span></td>
    </tr>

    <tr>
        <!-- <td width="30%" class="table_body">Your Email:</td>-->
        <td width="70%" class="table_body"><p><input name="youremail" type="text" size="30" value="" cellpadding=50 <?=stripslashes(htmlspecialchars($youremail));?>" /><span class="error_message">*</span></td>
    </tr>
    <tr>


            <?  If($use_subject_drop AND is_array($subjects)) {?>
                    <select name="emailsubject" size="1">
                        <?while(list($key,$val)=each($subjects)) {?>

                            <option value="<?=intval($key);?>"><?=htmlspecialchars(stripslashes($val));?></option>

                        <?}?>
                    </select>


            <?} Else {?>

            <?=stripslashes(htmlspecialchars($emailsubject));?>  

            <?}?>

        </td>

    </tr>

    <?For($i=1;$i <= $allowattach; $i++) {?>
        <tr>

            <!-- <td width="30%" class="table_body">Attach File:</td>-->
            <p><td width="100%" class="table_body"><p><input name="attachment[]" type="file" size="19" /></td>

        </tr>
    <?}?>

    <tr>


    </tr>
    <tr>
        <td colspan="2" width="100%" class="table_footer">
            <input type="hidden" name="submit" value="true" />
            <p><input type="submit" value="<?=$submitvalue;?>" /> &nbsp;
            <input type="reset" value="<?=$resetvalue;?>" />
        </td>
    </tr>
</table>
</form>

<?} Else {?>

    <div align="center" class="thanks_message"><?=$thanksmessage;?></div>
    <br />
    <br />

<script>location.href='http://www.mywebsitename/image_submitted.html'</script>

<?}
?>

0 个答案:

没有答案