Reputation: 309
Hi guys I have got a newsletter script which will send whatever the administrator has put in the text area to all the emails in the database, now I would like that user also be able to upload a .pdf file to that email, Any help much appreciated! here is the code of actually sending out the email:
Plain is the textarea with the message Subject is the subject Will need to add a upload input on the form which validates to this script
<?php
include "connect.php";
$subject = stripslashes($_POST["subject"]);
$plain = stripslashes($_POST["plain"]);
$result = mysql_query("SELECT email FROM member");
$emails = array();
while ($row = mysql_fetch_row($result))
$emails[] = $row[0];
$subject = $_POST['subject'];
$from = "[email protected]";
$headers = "From:" . $from;
$to = implode(", ", $emails);
mail($to, $_POST['subject'], $_POST["plain"], $headers);
?>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
<form id='register' action='updateprofile.php' method='post' accept-charset='UTF-8'>
<body id="main_body" >
<img id="top" src="top.png" alt="">
<div id="form_container">
<h1>Newsletter Sent</h1>
<form id="form_362567" class="appnitro" method="post" action="">
<div class="form_description">
<h2> Newsletter Sent</h2>
<p></p>
</div>
<ul >
<li class="section_break">
<p></p>
</li> <li id="li_2" >
<label class="description" for="email">
<?php
echo "Newsletter successfully sent, you will be redirected back to the member area in 5 seconds.";
?>
</form>
<div id="footer">
<meta http-equiv="refresh" content="5; URL=index.php">
</div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
Upvotes: 0
Views: 1863
Reputation: 489
You can try this
<?php
$fp = fopen('myfile.pdf','rb');
$dt = fread($fp,filesize('myfile.pdf'));
$attachments[] = Array(
'data' => $dt,
'name' => $name,
'type' => 'application/pdf'
);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$emailcontent="<style type='text/css'>
body {
font-family: arial, Geneva, sans-serif;
font-size: 12px;
}
</style>";
$subject = 'Subject';
$my_message = 'My message';
$headers = "MIME-Version: 1.0\n" .
"From: {$from}\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Add a multipart boundary above the plain message
$final_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$my_message . "\n\n";
//Add sttachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$final_message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data;
}
$final_message .= "--{$mime_boundary}--\n";
$emailto = '[email protected]';
mail($emailto, $subject, $final_message, $headers);
?>
I used this code a long time ago to attach attachments. I am not sure if it is still working.
Upvotes: 0
Reputation: 336
PEAR_mail is a simple and well supported replacement for the PHP built in mail function. It has support for mime encoding and attachments and excellent documentation for what you are trying to achieve.
Upvotes: 0
Reputation: 989
Use a library like SwiftMailer (http://swiftmailer.org/docs/messages.html)... much easier to add attachements.
Upvotes: 1