与PHPMailer联系表格

时间:2013-04-22 09:41:16

标签: php forms phpmailer

我有一个非常简单的表格,包括:

  • 名称
  • 电子邮件
  • 消息

我正在使用PHPMailer发送表单,因为我还希望它包含一个位于服务器上的附件(PDF)。

我已经将表单发送到可以发送姓名,电子邮件和附件 OR 名称,电子邮件和消息的位置。当一个人工作时似乎禁用另一个。

请参阅下面我正在使用的PHP代码:

<?php 

  $field_fullname = $_POST['cf_mercury'];
  $field_email = $_POST['cf_jupiter'];
  $field_message = $_POST['cf_uranus'];

//define the receiver of the email 
$to = 'email@emailaddress.co.uk'; 
//define the subject of the email 
$subject = 'Message via the website from '.$field_fullname;
//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 
$body_message = 'From: '.$field_fullname."\n";
$body_message .= 'Message: '.$field_message;
//Main message
$headers = "From: $field_email\r\nReply-To: $field_email"; 
//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
$attachment = chunk_split(base64_encode(file_get_contents('/websites/123reg/LinuxPackage22/da/mt/ec/damtechdesigns.co.uk/public_html/proofs/etap/etapbooklet.pdf'))); 
//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-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="ETAPBooklet.pdf"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

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

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

 if ($mail_sent) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for contacting the whoever. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
  else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send your email to email@emailaddress.co.uk');
        window.location = 'index.html';
    </script>
<?php
}

?>

代码当前状态是它发送附件但不显示消息。

非常感谢任何和所有的帮助,如果我能做更多的事情来表达我的问题并增加其可理解性,请告诉我。

解决方案:

<?php

  $field_fullname = $_POST['cf_mercury'];
  $field_email = $_POST['cf_jupiter'];
  $field_message = $_POST['cf_uranus'];

require_once('class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body = 'From: '.$field_fullname;
$body .= 'Message: '.$field_message;

$mail->SetFrom($field_email, $field_fullname);

$mail->AddReplyTo($field_email,$field_fullname);

$address = "email@address.co.uk";
$mail->AddAddress($address, "Name");

$mail->Subject    = 'Message via the website from '.$field_fullname;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("/websites/123reg/LinuxPackage22/da/mt/ec/damtechdesigns.co.uk/public_html/proofs/etap/etapbooklet.pdf");      // attachment
$mail->AddAttachment(""); // attachment

if(!$mail->Send()) {
  { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send your email to alternatemail@address.co.uk');
        window.location = 'index.html';
    </script>
<?php
}
} else {
  ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for contacting the ETAP Centre. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
?>

1 个答案:

答案 0 :(得分:1)

您提供的代码仅显示了发送包含单个附件的电子邮件的尝试,因此在发送邮件+附件时很难看出可能出现的问题。请注意,对于MIME,“message”本身就是一个内联处置的MIME附件。

只需使用swiftmailer或phpmailer,您就可以省去很多痛苦。