带有图像附件的表格邮件程序

时间:2015-09-16 11:41:21

标签: php html email

我制作了这种形式的邮件,它没有图像附件就很好用。但我还需要图片上传功能。这是一个简单的方法吗?

我已经尝试过一些东西,但我不能让它发挥作用。

提前致谢!



<?php

$EmailFrom = "mail@from.com";
$EmailTo = "your@mail.com";
$Subject = "New Mail";
$Name = Trim(stripslashes($_POST['Name'])); 
$City = Trim(stripslashes($_POST['City'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Position = Trim(stripslashes($_POST['Position']));


// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Wohnort: ";
$Body .= $City;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Position: ";
$Body .= $Position;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>G</title>
  </head>
  <body>
    <div class="container">
      <div class="main">
        <div class="position">
          
          <form method="post" action="contactengine.php" enctype="multipart/form-data"> >
            
            <label for="Name">Name:</label>
            <input type="text" name="Name" id="Name" required/>
        
            <label for="City">Wohnort:</label>
            <input type="text" name="City" id="Tel" required/>
  
            <label for="Email">Email:</label>
            <input type="text" name="Email" id="Email" required/>

            <label for="Tel">Tel:</label>
            <input type="text" name="Tel" id="Tel" />

            <input type="radio" name="Position" value="Pos1" required> Pos1
            <input type="radio" name="Position" value="Pos2" required> Pos2
            <label for="File">Filename:</label>
            <input type="file" name="File" id="File"><br>

            <input type="submit" name="submit" value="Submit" class="submit-button" />

          </form>

        
        </div>
      </div>

    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码发送带附件的邮件。我希望这段代码可以帮到你。

$mail_to = "";
$from_mail = "";
$from_name = "";
$reply_to = "";
$subject = "";
$message = "";

$file_name = "<attachment file name>";
$path = "<relative path the attachment>";

$file = $path.$file_name;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));

$boundary = md5(uniqid(time()));

$header = "From: ".$from_name." \r\n";
$header .= "Reply-To: ".$reply_to."\r\n";
$header .= "MIME-Version: 1.0\r\n";

$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"".$boundary."\"";

$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$boundary."\r\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$message_body\r\n";
$message .= "--".$boundary."\r\n";

$message .= "Content-Type: application/xml;\r\n";
$message .= " name=\"".$file_name."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"".$file_name."\"\r\n";
$message .= "\r\n".$content."\r\n";
$message .= "--".$boundary."--\r\n";

if (mail($mail_to, $subject, $message, $header)) {
    echo "Sent";
} else {
    echo "Error";
}

谢谢!