如何通过邮件发送文件附件以及php中的其他表单数据?

时间:2016-10-19 06:17:56

标签: php html

我有一个html表单,我要求我的用户填写他们的个人和职业细节并附上他们的简历。我想将这些细节(表格数据和附件)发送到使用PHP的电子邮件....任何帮助将不胜感激....

这是我的发送邮件的代码...使用这个我获取文件附件而不是其他表单数据...请帮助我出错的地方......

<?php
// Settings  -  working good
$name        = "Name goes here";
$email       = "test@gmail.com";
$to          = "$name <$email>";
$from        = "Vijay";
$subject     = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$attachments = $_FILES['attachment'];

if(empty($_POST['title'])        ||

  empty($_POST['exp'])            ||

  empty($_POST['skill'])         ||

  empty($_POST['qual'])         ||

  empty($_POST['certf'])         ||

  empty($_POST['domain'])        ||

  empty($_POST['fname'])        ||

  empty($_POST['lname'])        ||

  empty($_POST['phone'])        ||

  empty($_POST['email'])        ||

  empty($_POST['csal'])        ||

  empty($_POST['job'])            ||

 // empty($_POST['file'])        ||

  !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))

  {

  echo "No arguments Provided!";



  return false;

  }$title = strip_tags(htmlspecialchars($_POST['title']));

$exp = strip_tags(htmlspecialchars($_POST['exp']));

$skill = strip_tags(htmlspecialchars($_POST['skill']));

$qual = strip_tags(htmlspecialchars($_POST['qual']));

$certf = strip_tags(htmlspecialchars($_POST['certf']));

$domain = strip_tags(htmlspecialchars($_POST['domain']));

$fname = strip_tags(htmlspecialchars($_POST['fname']));

$lname = strip_tags(htmlspecialchars($_POST['lname']));

$phone = strip_tags(htmlspecialchars($_POST['phone']));

$email_address = strip_tags(htmlspecialchars($_POST['email']));

$csal = strip_tags(htmlspecialchars($_POST['csal']));

$job = strip_tags(htmlspecialchars($_POST['job']));



// File
 //Get uploaded file data
    $file_tmp_name    = $_FILES['attachment']['tmp_name'];
    $file_name        = $_FILES['attachment']['name'];
    $file_size        = $_FILES['attachment']['size'];
    $file_type        = $_FILES['attachment']['type'];
    $file_error       = $_FILES['attachment']['error'];

    if($file_error > 0)
    {
        die('Upload error or No files uploaded');
    }
    //read from the uploaded file & base64_encode content for the mail
    $handle = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

        $boundary = md5("sanwebe");
        //header
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

        //plain text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= chunk_split(base64_encode($Message));

        //attachment
        $body .= "--$boundary\r\n";
        $body .="Content-Type: $file_type; name=".$file_name."\r\n";
        $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
        $body .= $encoded_content;
$Message = "You have received a new resume from your website career application form.\n\n"."Here are the details:\n\n

Title: ".$title."\n
Experience: ".$exp."\n
Skill: ".$skill."\n
Qualification: ".$qual."\n
Domain: ".$domain."\n
First Name: ".$fname."\n
Last Name: ".$lname."\n
Phone: ".$phone."\n
Email: ".$email_address."\n\n

Current Salary: ".$csal."\n\n

Job: ".$job."\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $Message, $headers)) {
    echo "The email was sent.";
echo "$fileattname";
}
else {
    echo "There was an error sending the mail.";
}
?>

2 个答案:

答案 0 :(得分:0)

检查以下链接Send PHP mail with attachment

这就是你要找的东西

答案 1 :(得分:0)

您希望使用经过验证且经过良好测试的库,如Swiftmailer或Zend \ Mail,而不是像编写代码一样编写代码。