如何使用表单php在电子邮件中附加和发送文件

时间:2014-03-13 07:40:04

标签: php email email-attachments

在下面的代码中我有3个文件(job.php,form.php,sendmail.php)

在job.php我有工作提到,如果找工作者点击申请按钮,然后转到form.php,求职者在提交后填写他们的详细信息,然后将邮件发送给雇主。

在form.php中我有文件附件代码,其中求职者附上他们的简历 我不知道如何在电子邮件中附加文件(sendmail.php)

帮助我如何使用表单在电子邮件中附加和发送文件

job.php

    <form method="post" action="form.php" id="dataentry">
      <div class="jobopen">
            <h2>Urgent Opening for Data Entry</h2>
            <p>&nbsp;</p>
            <p> <strong>Job Description:</strong></p>
            <p>Dear candidate,Required for the job: 00-01 year 
          Data operator 6500 2nd and night shift required speed 25 wpm Experience. Annual Salary of the job: 185000 -240000
          Location: Maharashtra </p>
            <p><strong>PROFILE:</strong> Data Entry Executive 
          Qualification:Hsc Pass
          If you are interested, walk in at the following venue to kick start
          your career </p>
            <p>&nbsp; </p>
            <input type="submit" value="Apply"/>
          </div>
    </form>
        <br />
        <br />
        <form method="post" action="form.php">
      <div class="jobopen">
            <h2>Urgent Opening for Telecaller</h2>
            <p>&nbsp;</p>
            <p> <strong>Job Description:</strong></p>
            <p>Dear candidate,Required for the job: 00-01 year 
          Telecaller 6500 2nd and night shift required speed 25 wpm Experience. Annual Salary of the job: 185000 -240000
          Location: Maharashtra </p>
            <p><strong>PROFILE: </strong>Telecaller Qualification:Hsc Pass
          If you are interested, walk in at the following venue to kick start
          your career </p>
            <p>&nbsp; </p>
            <input type="submit" value="Apply"/>
          </div>
    </form>

form.php的

<form action="sendmail.php" method="post" enctype="multipart/form-data" >
      <label>Name:</label>
      <input type="text" name="name"  />
      <br />
      <br />
      <label>Email-ID:</label>
      <input type="text" name="email"/>
      <br />
      <br />
      <label>Phone No:</label>
      <input type="text" name="phone" />
      <br />
      <br />
      <label>Upload Resume</label>
      <input type="file" name="file" id="file"/><br /><br />   
      <input style="display:block; margin-left:35em;"type="submit" value="Submit"/>
    </form>

sendmail.php

<?php
$to = "admin@companywebsite.co.in";

        // Change this to your site admin email

        $from = "$_POST[email]";

        $subject = "online Job Register form FROM $_POST[name] ";

        //Begin HTML Email Message where you need to change the activation URL inside

        $message = '<html>

        <head>

        <style>

label {

      width: 10em;

      float: left;

      text-align: right;

      margin-right: 2em;

      display: block;

      }

        </style>

        </head>

        <body bgcolor="#ffffff">


        <label>Name:</label>'.$_POST['name'].'<br /><br />

        <label>E-mail Address:</label>'.$_POST['email'].'<br /><br />

        <label>Mobile No:</label>'.$_POST['phone'].'<br/><br />

<br />



  <br />

        Thanks! <br />

        '.$_POST['name'].'

        </body>

        </html>';

        // end of message

        $headers = "From: $from\r\n";

        $headers .= "Content-type: text/html\r\n";

        $to = "$to";

        // Finally send the activation email to the member

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

        // Then print a message to the browser for the joiner 



print "<br /><br /><br /><h4>Thank you for contacting us. We will be in touch with you very soon</h4><br />";


?>

1 个答案:

答案 0 :(得分:4)

你可以通过以下方式做到这一点;

function sendMailWithAttachment($file, $to, $from, $from_name, $replyto, $subject, $message) {
    $hash = md5(uniqid(time()));

    $header = "From: ".$from_name." <".$from.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$hash."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$hash."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    $filename = basename($file);
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $header .= "--".$hash."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";

    $header .= "--".$hash."--";
    // $message is already in header
    return mail($to, $subject, "", $header);
}

我假设您已将文件发送到php,并在服务器端成功获取文件。之后,您可以将该文件提供给上述邮件功能