将上传的文件作为附件发送到电子邮件 [英] Sending an uploaded file as attachment to email

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

问题描述

我是网页设计的新手。我需要用HTML创建表单,以便用户输入几个字段并上传他/她的简历。当他提交表格时,他的提交应该通过电子邮件将他的简历作为附件发送给我。
我用PHP发送邮件。一切正常,除了文件没有附加发送的电子邮件。



我发布HTML和PHP代码,请帮助我..

HTML代码:FileName:Careers.html

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>联络表单< / title>
< / head>

< body>
< form action =Careers.phpmethod =postenctype =multipart / form-data>
名称< br>
< input type =textname =cf_name>< br>
电子邮件< br>
< input type =textname =cf_email>< br>
出生日期< br>
< input type =textname =db_name>< br>
联络电话号码< br>
< input type =textname =db_cntct>< br>
Class 12 Marks / CGPA / Percentage< br>
< input type =textname =cf_board>< br>
毕业标记/ CGPA /百分比< br>
< input type =textname =db_grad>< br>
毕业后标记/ CGPA /百分比< br>
< input type =textname =cf_pgrad>< br>
现在雇主< br>
< input type =textname =db_emplyr>< br>
加入日期< br>
< input type =textname =cf_doj>< br>
名称< br>
< input type =textname =db_desg>< br>
当前CTC< br>
< input type =textname =db_ctc>< br>
上传您的简历< br>
< input type =filename =attachmentsize =40>< br>
消息< br>
< textarea name =cf_message>< / textarea>< br>< br>
< input type =submitvalue =发送>
< input type =resetvalue =清除>
< / form>
< / body>
< / html>

PHP代码:FileName:Careers.php

 <?php 
$ field_name = $ _POST ['cf_name'];
$ field_email = $ _POST ['cf_email'];
$ field_dob = $ _POST ['db_name'];
$ field_contact = $ _POST ['db_cntct'];
$ field_board = $ _POST ['cf_board'];
$ field_grad = $ _POST ['db_grad'];
$ field_pgrad = $ _POST ['cf_pgrad'];
$ field_emplyr = $ _POST ['db_emplyr'];
$ field_doj = $ _POST ['cf_doj'];
$ field_desg = $ _POST ['db_desg'];
$ field_ctc = $ _POST ['db_ctc'];
$ field_message = $ _POST ['cf_message'];

$ mail_to ='sachinrocksus@gmail.com';
$ subject ='来自网站访问者的工作申请'$ field_name;

$ body_message ='From:'。$ field_name。\\\
;
$ body_message。='E-mail:'。$ field_email。\\\
;
$ body_message。='出生日期:'$ field_dob。\\\
;
$ body_message。='Contact Number:'。$ field_contact。\\\
;
$ body_message。='Class 12 Marks / CGPA / Percentage:'。$ field_board。\\\
;
$ body_message。='毕业标记/ CGPA /百分比:'。$ field_grad。\\\
;
$ body_message。='毕业后标记/ CGPA /百分比:'。$ field_pgrad。\\\
;
$ body_message。='Present Employer:'。$ field_emplyr。\\\
;
$ body_message。='加入日期:'。$ field_doj。\\\
;
$ body_message。='指定:'$ field_desg。\\\
;
$ body_message。='当前CTC:'$ field_ctc。\\\
;
$ body_message。='Message:'。$ field_message。\\\
;

$ headers ='From:'。$ field_email。\r\\\
;
$ headers。='Reply-To:'。$ field_email。\r\\\
;

$ mail_status = mail($ mail_to,$ subject,$ body_message,$ headers);

if($ mail_status){?>
< script language =javascripttype =text / javascript>
alert(您的工作申请已经收到,我们会尽快与您联系。);
window.location ='Careers.html';
< / script>
<?php
}
else {?>
< script language =javascripttype =text / javascript>
alert('Message failed');
window.location ='Careers.html';
< / script>
<?php
}
?>


解决方案

你没有作为附件传递



这里我粘贴一个代码片段希望这将有助于您

 <?php 
$ fileatt =mypdffile.pdf; //文件路径
$ fileatt_type =application / pdf; //文件类型
$ fileatt_name =mypdffile.pdf; //将文件用作附件的文件名

$ email_from =sales@mysite.com; //电子邮件来自
$ email_subject =您附加的文件; //电子邮件的主题
$ email_message =感谢您访问mysite.com!这是您的免费文件
;
$ email_message。=感谢您访问
; //电子邮件中的消息

$ email_to = $ _POST ['email']; //电子邮件是谁

$ headers =From:$ email_from;

$ file = fopen($ fileatt,'rb');
$ data = fread($ file,filesize($ fileatt));
fclose($ file);

$ semi_rand = md5(time());
$ mime_boundary === Multipart_Boundary_x {$ semi_rand} x;

$ headers。=\\\
MIME-Version:1.0\\\

Content-Type:multipart / mixed; \\\

boundary = \{$ mime_boundary} \;

$ email_message。=这是MIME格式的多部分消息.\\\
\\\

- {$ mime_boundary} \\\

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

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

$ email_message。=\\\
\\\
;

$ data = chunk_split(base64_encode($ data));

$ email_message。= - {$ mime_boundary} \\\

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

name = \{$ fileatt_name} \\\\

//Content-Disposition:attachment; \\\

//filename = \{$ fileatt_name} \\\\

Content-Transfer-Encoding:base64\\\
\\\

$ data。=\\\
\\\

- {$ mime_boundary} - \\\
;

$ ok = @mail($ email_to,$ email_subject,$ email_message,$ headers);

如果($ ok){
echo您的文件已经发送
到您指定的电子邮件地址

确保检查你的垃圾邮件!

点击这里返回mysite.com。;

} else {
die(抱歉,电子邮件无法发送,请回去再试一次!
}
?>


I am a novice in Web designing. I need to create a form in HTML such that it asks the user to enter several fields and upload his/her resume. When he submits the form, his submissions should be email to me with his resume as the attachment with the email. I have used PHP for sending the email. Everything works fine, except that the file is not getting attached with the sent email.

I am posting both the HTML and the PHP code, please help me..

HTML Code: FileName: Careers.html

    <!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form</title>
</head>

<body>
<form action="Careers.php" method="post" enctype="multipart/form-data">
    Name<br>
     <input type="text" name="cf_name"><br>
    E-mail<br>
     <input type="text" name="cf_email"><br>
        Date of Birth<br>
     <input type="text" name="db_name"><br>
        Contact Number<br>
     <input type="text" name="db_cntct"><br>
    Class 12 Marks/CGPA/Percentage<br>
     <input type="text" name="cf_board"><br>
         Graduation Marks/CGPA/Percentage<br>
     <input type="text" name="db_grad"><br>
     Post-Graduation Marks/CGPA/Percentage<br>
     <input type="text" name="cf_pgrad"><br>
        Present Employer<br>
     <input type="text" name="db_emplyr"><br>
     Date of Joining<br>
     <input type="text" name="cf_doj"><br>
        Designation<br>
     <input type="text" name="db_desg"><br>
        Current CTC<br>
     <input type="text" name="db_ctc"><br>
     Upload your Resume<br>
     <input type="file" name="attachment" size="40"><br>
Message<br>
<textarea name="cf_message"></textarea><br><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
</body>
</html>

PHP Code: FileName: Careers.php

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_dob = $_POST['db_name'];
$field_contact = $_POST['db_cntct'];
$field_board = $_POST['cf_board'];
$field_grad = $_POST['db_grad'];
$field_pgrad = $_POST['cf_pgrad'];
$field_emplyr = $_POST['db_emplyr'];
$field_doj = $_POST['cf_doj'];
$field_desg = $_POST['db_desg'];
$field_ctc = $_POST['db_ctc'];
$field_message = $_POST['cf_message'];

$mail_to = 'sachinrocksus@gmail.com';
$subject = 'Job Application from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Date of Birth: '.$field_dob."\n";
$body_message .= 'Contact Number: '.$field_contact."\n";
$body_message .= 'Class 12 Marks/CGPA/Percentage: '.$field_board."\n";
$body_message .= 'Graduation Marks/CGPA/Percentage: '.$field_grad."\n";
$body_message .= 'Post-Graduation Marks/CGPA/Percentage: '.$field_pgrad."\n";
$body_message .= 'Present Employer: '.$field_emplyr."\n";
$body_message .= 'Date of Joining: '.$field_doj."\n";
$body_message .= 'Designation: '.$field_desg."\n";
$body_message .= 'Current CTC: '.$field_ctc."\n";
$body_message .= 'Message: '.$field_message."\n";

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";       

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Your Job Application has been recieved. We will contact you shortly.');
        window.location = 'Careers.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed');
        window.location = 'Careers.html';
    </script>
<?php
}
?>

解决方案

You are passing nothing as an attachment

Here i am pasting a snippet hope this will help you

<?php
    $fileatt = "mypdffile.pdf"; // Path to the file
    $fileatt_type = "application/pdf"; // File Type
    $fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment

    $email_from = "sales@mysite.com"; // Who the email is from
    $email_subject = "Your attached file"; // The Subject of the email
    $email_message = "Thanks for visiting mysite.com! Here is your free file.
    ";
    $email_message .= "Thanks for visiting.
    "; // Message that the email has in it

    $email_to = $_POST['email']; // Who the email is to

    $headers = "From: ".$email_from;

    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message .= "\n\n";

    $data = chunk_split(base64_encode($data));

    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";

    $ok = @mail($email_to, $email_subject, $email_message, $headers);

    if($ok) {
    echo "You file has been sent
    to the email address you specified.

    Make sure to check your junk mail!

    Click here to return to mysite.com.";

    } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
    }
    ?> 

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

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