0

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

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

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

下面的代码是页面的完整代码。很抱歉,这有点多,但我不知道哪里出了问题,所以我不能只强调其中的一部分。感谢所有回复这篇文章的人。

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

问题:为什么不发送附件和文本区域。

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. 太糟糕了,它没有用。这是一种蹩脚的尝试和错误。但不努力的人永远不会成功。

4

1 回答 1

1

我没有让它工作,所以我切换到了 Chronoforms,它是一个 Joomla 扩展,我发现这不仅更容易,而且也更快。我邀请大家提出一个适当的解决方案来解决上述问题。但与此同时,当您可以使用 Chronoforms 时。我建议这样做。

于 2013-10-25T16:00:14.177 回答