我有一个非常简单的表格,包括:
- 姓名
- 电子邮件
- 信息
我正在使用 PHPMailer 发送表单,因为我还希望它包含位于服务器上的附件 (PDF)。
我已经把表格弄到了可以发送姓名、电子邮件和附件或姓名、电子邮件和消息的地步。当一个工作时,它似乎禁用另一个。
请参阅下面我正在使用的 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
}
?>