在电子邮件中显示附件或消息,但不是两者都显示

时间:2013-10-21 13:29:18

标签: php email textarea send email-attachments

当我通过以下代码生成的页面发送电子邮件时。我遇到以下问题。

当我填写文本区域并添加附件时,接收的消息是除主体(文本区域)之外的所有内容。

当我不添加附件时,它工作正常,但显然需要添加附件。

下面的代码是页面的完整代码。我很抱歉这是相当多的但我不知道哪里出错了所以我不能只突出它的一部分。感谢所有回复此帖子的人。

电子邮件:Swiftmailer,PHP Mailer Zend_Mail。我一直试图让他们上班,但我无法理解。简单的教程可行,但是从数据库和文件附件中获取内容变得无望。我确信这是可能的,但我不知道。

问题:为什么不发送附件和textarea。

verzenden.php

 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <link rel="stylesheet" type="text/css" href="css/overzichten.css">
 <link rel="stylesheet" type="text/css" href="css/offerte_facturen.css">
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">     </script>
 <style>.hidden{display: none;}</style>
 </head>
 <body>
 <?php
 // Load Joomla! configuration file
 require_once('../../../configuration.php');
 // Create a JConfig object
 $config = new JConfig();
 // Get the required codes from the configuration file
 $server = $config->host;
 $username   = $config->user;
 $password   = $config->password;
 $database = $config->db;
 // Connect to db
 $con = mysqli_connect($server,$username,$password,$database);
 if (!$con){
die('Could not connect: ' . mysqli_error($con));
 }
 mysqli_select_db($con,$database);

 // Check whether the value for id is transmitted
 if (isset($_GET['id'])) {

 // Put the value in a separate variable
 $id = $_GET['id'];

 // Query the database for the details of the chosen id
 $result = mysqli_query($con,"SELECT * FROM cypg8_overzicht WHERE id = $id");

 // Check result
 // This shows the actual query sent to MySQL, and the error. Useful for debugging.
 if (!$result) {
 $message = "Invalid query: " . mysqli_error($result) . "\n";
 $message .= "Whole query: " . $query;
 die($message);
 }

 // Use result
 // Attempting to print $result won't allow access to information in the resource
 // One of the mysql result functions must be used
 // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
 while ($row = mysqli_fetch_assoc($result)) {
 // ID van de offerte/factuur dit gegeven is verborgen van het formulier
 echo "<input type='text' name='id' id='id' style='display:none;' value='" .$row['id'].      "'>";

 // Start formulier
 echo "<form action='verzenden_script.php' method='post' name='form1' id='form1'      enctype='multipart/form-data'>";
 echo "<div>klantemail<input type='textbox' name='klantemail'   value='".$row['email']."'></div>";
 echo "<div>dealeremail<input type='textbox'    name='dealeremail' value='".$row['dealeremailadres']."'></div>";
 echo "<div><select name='offertefactuur'>";
 echo "<option value='Offertenummer'>Offerte</option>";
 echo "<option value='Factuurnummer'>Factuur</option>";
 echo "</select></div>";
 echo "<div>formuliernummer<input type='textbox'    name='formuliernummer' value='".$row['formuliernummer']."'></div>";
 echo "<div><li>Bedankt voor uw factuuraanvraag!</li></div>";
 echo "<div>berichtonderwerp<input type='textbox' name='berichtonderwerp'   value=''>     </div>";
 echo "<div><li>Bedankt voor uw factuur aanvraag. In de bijlage kunt u deze factuur bekijken.</li></div>";
 echo "<div>bericht<textarea    name='bericht'></textarea></div>";
 echo "<div><input type='file' name='fileAttach' value='Zoeken'></div>";
 echo "<div><input type='submit' name='Submit' value='Verzenden'></div>";
 echo "</form>";

 }
 } else {
 die("No valid id specified!");
 }
 ?>
 </body>

verzenden_script.php

<?php
$naar = $_POST["klantemail"];
$naar2 = $_POST["dealeremail"];
$naar3 = 'dealer@loginsecure.nl';
$formuliernummer = $_POST["formuliernummer"];
$offertefactuur = $_POST["offertefactuur"];
$berichtonderwerp = $_POST["berichtonderwerp"];
$bericht = $_POST["bericht"];

//define the receiver of the email 
$to = $naar.",".$naar2.",".$naar3; 
//define the subject of the email 
$subject = $berichtonderwerp ."|". $offertefactuur .":". $formuliernummer; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: $cc\r\nReply-To: $cc"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $headers .= "--".$strSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $headers .= $strContent."\n\n";
}
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php echo $berichtonderwerp ?>
<?php echo $bericht ?>

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2><?php echo $berichtonderwerp ?></h2> 
<p><?php echo $bericht ?></p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

修改1

我一直在阅读和阅读,很多人都遇到了同样的问题。那里有一些建议的部分

--PHP-alt-<?php echo $random_hash; ?>
--PHP-alt-<?php echo $random_hash; ?>--

错了,但我查了一下,我不相信问题就在那里。但经过一些阅读后,我认为问题出现在\n我希望这些问题需要改为\r\n。太糟糕了它没用。这是一种蹩脚的尝试和错误。但谁不尝试永远不会成功。

1 个答案:

答案 0 :(得分:1)

我没有让这个工作,所以我切换到Chronoforms,这是一个Joomla扩展,我发现这不仅更容易,但也更快。我邀请大家提出一个解决上述问题的正确解决方案。但同时当你处于使用Chronoforms的位置时。我建议这样做。