PHP表单上传和电子邮件 [英] Php form upload and email

查看:98
本文介绍了PHP表单上传和电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,理想情况下,用户需要在提交表单时上传文件(图片/视频),但这并不是强制性的。

 <?php 

$ to =someone@example.com;
$ fromEmail = $ _POST ['fieldFormEmail'];
$ fromName = $ _POST ['fieldFormName'];
$ subject = $ _POST ['fieldSubject'];
$ message = $ _POST ['fieldDescription'];

/ *获取文件变量* /
$ tmpName = $ _FILES ['attachment'] ['tmp_name'];
$ fileType = $ _FILES ['attachment'] ['type'];
$ fileName = $ _FILES ['attachment'] ['name'];

/ *开始标题* /
$ headers =From:$ fromName;
$ b $ if(file($ tmpName)){
/ *读取文件('rb'=读取二进制文件)* /
$ file = fopen($ tmpName,'rb' );
$ data = fread($ file,filesize($ tmpName));
fclose($ file);

/ *一个边界字符串* /
$ randomVal = md5(time());
$ mimeBoundary === Multipart_Boundary_x {$ randomVal} x;

/ *文件附件头* /
$头。=\\\
MIME-Version:1.0 \\\
;
$ headers。=Content-Type:multipart / mixed; \\\
;
$ headers。=boundary = \{$ mimeBoundary} \;

/ *多部分边界上面的消息* /
$ message =这是一个MIME格式的多部分消息。\\\ n。
- {$ mimeBoundary} \\\

Content-Type:text / plain; charset = \iso-8859-1 \\\\

Content-Transfer-Encoding:7bit \\\
\\\

$消息。 \\\
\\\
;

/ *编码文件数据* /
$ data = chunk_split(base64_encode($ data));

/ *在消息中添加attchment-file * /
$ message。= - {$ mimeBoundary} \\\

Content-Type:{$ fileType}; \\\

name = \{$ fileName} \\\\

Content-Transfer-Encoding:base64 \\\ n。
$数据。 \\\
\\\

- {$ mimeBoundary} - \\\
;


$ flgchk = mail($ to,$ subject,$ message,$ headers);

if($ flgchk){
echo电子邮件已发送到:$ to;
}
else {
echo电子邮件发送错误;
}
?>

< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>电子邮件附件没有上传 - 卓越的网络世界< / title>
< style>
body {font-family:Arial,Helvetica,sans-serif; font-size:13px;}
th {background:#999999;文本对齐:权利; vertical-align:top;}
input {width:181px;}
< / style>
< / head>
< body>
< form action =Contact.phpmethod =postname =mainformenctype =multipart / form-data>
< table width =500border =0cellpadding =5cellspacing =5>
< tr>
< th>您的名字< / th>
< td><输入名称=fieldFormNametype =text>< / td>
< / tr>
< tr>
< tr>
您的电子邮件地址< / th>
< td>< input name =fieldFormEmailtype =text>< / td>
< / tr>

< tr>
< th> Subject< / th>
< td>< input name =fieldSubjecttype =textid =fieldSubject>< / td>
< / tr>
< tr>
< th>评论< / th>
< td>< textarea name =fieldDescriptioncols =20rows =4id =fieldDescription>< / textarea>< / td>
< / tr>
< tr>
< th>附加档案< / th>
< td><输入名称=附件type =file>< / td>
< / tr>
< tr>
< td colspan =2style =text-align:center;>< input type =submitname =Submitvalue =Send>< input type =重置名称=重置值=重置>< / td>
< / tr>
< / table>
< / form>
< / body>
< html>

我面对的问题是,


  1. 它会在页面加载时运行,而
  2. 如果文件尚未上传,它会引发警告。
  3. >

    我试图将文件上传部分放在if filename exists的条件下,但是电子邮件不发送附件
    请帮忙。
    此文件名为Contact.php

    解决方案


    它在页面加载时运行

    $ b

    附上邮件发送功能

    pre $ if(isset($ _ POST ['Submit'])&&($ _POST ['Submit'])=='Send')
    {
    / *仅当提交按钮名称= '提交'
    和value ='发送'被按下
    整个PHP代码* /
    }




    如果文件尚未上传,则会引发警告。


    在将它附加到PHP之前进行检查

      if((empty($ _ POST ['attachment']))||(empty($ _FILES ['attachment']))){
    //文件未附加,显示错误
    } else {
    //文件已附加,处理并通过邮件发送
    }


    I have the following code, ideally, it is expected that the user shall upload a file(image/video) when submitting the form, but it is not compulsory though.

    <?php
    
    $to = "someone@example.com";
    $fromEmail = $_POST['fieldFormEmail']; 
    $fromName = $_POST['fieldFormName']; 
    $subject = $_POST['fieldSubject']; 
    $message = $_POST['fieldDescription'];
    
    /* GET File Variables */ 
    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 
    
    /* Start of headers */ 
    $headers = "From: $fromName"; 
    
    if (file($tmpName)) { 
      /* Reading file ('rb' = read binary)  */
      $file = fopen($tmpName,'rb'); 
      $data = fread($file,filesize($tmpName)); 
      fclose($file); 
    
      /* a boundary string */
      $randomVal = md5(time()); 
      $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 
    
      /* Header for File Attachment */
      $headers .= "\nMIME-Version: 1.0\n"; 
      $headers .= "Content-Type: multipart/mixed;\n" ;
      $headers .= " boundary=\"{$mimeBoundary}\""; 
    
      /* Multipart Boundary above message */
      $message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mimeBoundary}\n" . 
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $message . "\n\n"; 
    
      /* Encoding file data */
      $data = chunk_split(base64_encode($data)); 
    
      /* Adding attchment-file to message*/
      $message .= "--{$mimeBoundary}\n" . 
      "Content-Type: {$fileType};\n" . 
      " name=\"{$fileName}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" . 
      "--{$mimeBoundary}--\n"; 
    } 
    
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
    
    if($flgchk){
      echo "A email has been sent to: $to";
     }
    else{
      echo "Error in Email sending";
    }
    ?>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Email Attachment Without Upload - Excellent Web World</title>
    <style>
    body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
    th{ background:#999999; text-align:right; vertical-align:top;}
    input{ width:181px;}
    </style>
    </head>
    <body>
        <form action="Contact.php" method="post" name="mainform" enctype="multipart/form-data">
        <table width="500" border="0" cellpadding="5" cellspacing="5">
           <tr>
            <th>Your Name</th>
            <td><input name="fieldFormName" type="text"></td>
        </tr>
        <tr>
        <tr>
            <th>Your Email</th>
            <td><input name="fieldFormEmail" type="text"></td>
        </tr>
    
        <tr>
            <th>Subject</th>
            <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
        </tr>
        <tr>
            <th>Comments</th>
            <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
        </tr>
        <tr>
          <th>Attach Your File</th>
          <td><input name="attachment" type="file"></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
        </tr>
        </table>
        </form>
    </body>
    <html>
    

    The problem that i am facing is,

    1. it runs on page load, and
    2. if file has not been uploaded, it throws a warning.

    I tried putting the file upload part under "if filename exists" condition, but then the email does not send the attachment Please help. This file name is Contact.php

    解决方案

    it runs on page load

    enclose mail send feature in

    if(isset($_POST['Submit']) && ($_POST['Submit']) == 'Send' )
    {
      /* process only when submit button whose name='Submit' 
         and value= 'Send' is pressed
         entire PHP code */
    }
    

    if file has not been uploaded, it throws a warning.

    check before attaching it in PHP

    if((empty($_POST['attachment'])) || (empty($_FILES['attachment']))){
        //file is not attached, show error
    }else{
     //file is attached, process it and send via mail
    }
    

    这篇关于PHP表单上传和电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆