I have a very simple form consisting of:
- Name
- Message
I am using PHPMailer to send the form because I also want it to include an attachment (PDF) which sits on the server.
I have gotten the form to a point where it can send either the name, email and attachment OR name, email and message. When one works it seems to disable the other.
Please see below the PHP code I am using:
<?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
}
?>
The codes current state is that it sends the attachment but does not display the message.
Any and all help would be hugely appreciated and if I can do anything more to articulate my question and increase its understandability just let me know.
Solution:
<?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
}
?>