Reputation: 215
I am trying to make a script to send emails and attach pdfs or other types of files later on.
I have been following various tutorials and have come to a dead end.
Can anyone see anything wrong with my code? I think i have just been looking at it for far too long.
<?php
include ("../../includes/auth.php");
$id = $_GET['id'];
$date = date('y-m-d h:i:s');
$recipient = $_GET['email'];
$lname = $_GET['lname'];
$fname = $_GET['fname'];
$subject ="Enquiry Form - $date";
//-------------attachment stuff----------
$fileatt = "/test.pdf";
$fileatttype = "application/pdf";
//$fileattname = "newname.pdf";
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//-------------------------------------
$headers = "From: test person <[email protected]>";
$headers .= "\nMIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n".
"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n". $message ."\n\n";
$data = chunk_split(base64_encode($data));
$message .= "–-{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ."--{$mime_boundary}--\n";
$message ."Dear ".$fname." ".$lname." Test message".$date;
echo $recipient."<br />";
echo $subject."<br />";
echo $message."<br />";
echo $headers."<br />";
mail($recipient, $subject, $message, $headers);
include ("../../includes/dbconn.php");
$set_datesent = "UPDATE _leads SET `Covering_letter_sent` = '$date' WHERE `ID` = '$id'";
//echo $set_datesent;
$result = mysql_query ($set_datesent, $connection)
or die ("Failed to perform query : $sql <br />".mysql_error());
?>
Upvotes: 0
Views: 630
Reputation: 2841
I would suggest you use something like the PEAR Mail library. Then sending an attachment becomes as easy and readable as the code below. You'll have to make sure the Mail library is installed, but this is a fairly simple task.
require_once('Mail.php');
require_once('Mail/mime.php');
$text = wordwrap($message, 70); // You'd put the text version of your message here as $message
$mime = new Mail_Mime();
$mime->setTXTBody($text);
$mime->setFrom($name . ' <' . $from_email . '>'); // The sender name and email ID go here
$mime->addAttachment($fpath, $ftype); // File path and type go here
$mime->setSubject($subject); // Subject goes here
$body = $mime->get()
$headers = $mime->headers();
$mail =& Mail::factory('mail');
$mail->send($to_email, $headers, $body); // The recipients email address goes here
unset($mime, $mail);
Additional things also become much easier this way, for example, writing a HTML mail.
Upvotes: 1
Reputation: 828
I'd suggest Pear Mail_mime package (http://pear.php.net/package/Mail_Mime/). I once did attachment handling by hand, because i didn't know such library exists (stupid me) and when i figured it out, i immediately got rid of my poorly made script and started to use Mail_mime. It has few glitches, but heck, it's so much easier than code it by hand.
Upvotes: 0
Reputation: 62384
I would highly recommend using a library like SwiftMailer. It's free and simplies your life by a lot.
Upvotes: 0