与PHPMailer的联系表(Contact Form with PHPMailer)

2019-10-18 01:17发布

我有一个非常简单的形式包括:

  • 名称
  • 电子邮件
  • 信息

我使用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
}
?>

Answer 1:

你只已经给出的代码显示了atempt与单个附件的电子邮件所以这是相当困难的时候看到你发送邮件附件+可能什么错。 需要注意的是与MIME,“消息”是本身MIME附件与处置直列。

您可以通过简单地使用swiftmailer或PHPMailer的自己节省了不少的苦。



文章来源: Contact Form with PHPMailer