如何使用我的表单将附件文件包含在电子邮件中?

时间:2013-08-10 08:43:04

标签: php javascript html forms email-attachments

我有一张表格:

<form id='contactus' action='send.php' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>
<fieldset>
<legend>My Form</legend>
<table><tbody>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='name' >Name: </label></td>
<td><input type='text' name='name' id='name' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='email' >Email: </label></td>
<td><input type='text' name='email' id='email' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='link' >Link: </label></td>
<td><input type='text' name='link' id='link' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='subject' >Subject: </label> 
</td><td><input type='text' name='subject' id='subject' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='description' >Description: 
</label></td>
<td><textarea rows="10" cols="50" name='description' id='description'></textarea>
</td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='photo' >Photo: </label></td>
<td><input type="file" name='photo' id='photo' /></td></tr>
</tbody></table>
<td nowrap="nowrap" style="text-align: center;"><input type='submit' value='Send' /></td>
</fieldset>
</form>

我的'send.php'是

<?php
if(isset($_POST['email'])) {

$email_to = "mymail@gmail.com";
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$link = $_POST['link'];
$description = $_POST['description'];
$photo = $_POST['photo'];

$email_message .= "Name: ".($name)."\n";
$email_message .= "Email: ".($email)."\n";
$email_message .= "Link: ".($link)."\n";
$email_message .= "Subject: ".($subject)."\n";
$email_message .= "Description: ".($description)."\n";
$email_message .= "Photo: ".($photo)."\n";

// create email headers
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message);
}
header("Location: thank-you.html");
?>

我的问题是:

1)一切都很好..除了'照片',我知道它是一个附件。但我不知道如何制作它?

2)当我收到电子邮件时,我怎么能看到这个粗体?

$ email_message。=“姓名:”。($ name)。“\ n”;

emp:名称:约翰

我试着这样做,但没有用。

$email_message .= "<b>Name: </b>".($name)."\n";
$email_message .= "<b>Email: </b>".($email)."\n";
$email_message .= "<b>Link: </b>".($link)."\n";
$email_message .= "<b>Subject: </b>".($subject)."\n";
$email_message .= "<b>Description: </b>".($description)."\n";

提前致谢

2 个答案:

答案 0 :(得分:0)

使用$ _FILES超级全局数组而不是$ _POST来处理文件。

 $photo = $_FILES['photo']['name'];
    ...

 $email_message .= "Photo: ".$photo."\n";

答案 1 :(得分:0)

您必须使用$ _FILES指定文件。

 $name_of_uploaded_file =
        basename($_FILES['photo']['name']); // get name of file uploaded

    //get the file extension of the file
    $type_of_uploaded_file =
        substr($name_of_uploaded_file,
        strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file =
        $_FILES["photo"]["size"]/1024;//size in KBs

    //copy the temp. uploaded file to uploads folder

    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = $_FILES["photo"]["tmp_name"];

    if(is_uploaded_file($tmp_path))
    {
      if(!copy($tmp_path,$path_of_uploaded_file))
      {
        $errors .= '\n error while copying the uploaded file';
      }
    }

首先,我们需要为这些类包含pear库文件。

include_once('Mail.php');
include_once('Mail_Mime/mime.php');


$message = new Mail_mime();

$message->setTXTBody($text);

$message->addAttachment($path_of_uploaded_file);

$body = $message->get();

$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);

$headers = $message->headers($extraheaders);

$mail = Mail::factory("mail");

$mail->send($to, $headers, $body);