Page 1 of 1

Getting attachements to send- Pear

Posted: Mon Jul 20, 2009 12:14 pm
by Addos
Hi,
I’m using a script below to good success but when trying to add an attachment to send it’s not sending this but the email that is in the queue comes through.
I’m really struggling to understand and follow how to add the $mime->addAttachment($file,'image/jpeg'); part of this script to function with the rest of the script and would really appreciate some direction.

I have a form that adds all the content to a table and I have no problem in uploading a file to a directory with permissions to have this attached to the mail in the queue but I’m not sure if that is what I should be doing. Any info I look up on the web shows the script that sends the mail but not the form it might use to upload the attachments. I’m not familiar with Pear and wonder if I need to add much in compassion to if I was using php’s mail() on it’s own.

If anyone can advise me I’d appreciate this as I’m at this for a long time and can’t figure it out.

I think the main thing I need oto do is add

Code: Select all

$file = 'uploads/sample.jpg';
$crlf = "rn";
 
$mime = new Mail_mime($crlf); 
$mime->setTXTBody($text);  
$mime->setHTMLBody($html);
$mime->addAttachment($file,'image/jpeg');
Somewhere into the script below but as I say at it stands the mail sends but no attachment.

Thanks for any help

Code: Select all

<?PHP
 
/* PHP email queue processing script */
require_once "Mail.php";
include('Mail/mime.php');
 
// defile variables
$db_url = "****t";
$db_name = "*****";
$db_user = "*****";
$db_pass = "******";
 
$host = "localhost";//MAIL_SERVER
$username = "info@blah.com";//MAIL_USER
$password = "******";//MAIL_PASS
 
$max_emails_per_batch = 15; 
 
// connect to database
$link = mysql_connect($db_url, $db_user, $db_pass);
if (!$link) {
die('Could not receive connection: ' . mysql_error());
}
if (!mysql_select_db($db_name, $link)) {
die('Could not connect to db: ' . mysql_error());
}
 
// query email_queue for records where success = 0
$sql = "SELECT * FROM email_queue WHERE success = 0 AND max_attempts != attempts LIMIT " . $max_emails_per_batch;
$result = mysql_query($sql, $link);
 
if (!$result) {
//echo "DB Error, could not query the databasen";
//echo 'MySQL Error: ' . mysql_error();
exit;
}
 
// check if records found
if (mysql_num_rows( $result )) {
 
// prepare mailer
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
 
// loop through records to send emails
while ($queued_mail = mysql_fetch_array($result)) {
// send email
 
$to =        $queued_mail['to_email'];
$subject =   $queued_mail['subject'];
$body =      $queued_mail['message'];
$from =      $queued_mail['from_name'] . ' < ' . $queued_mail['from_email'] . '>';
 
$file = 'uploads/sample.jpg';
$crlf = "rn";
 
$mime = new Mail_mime($crlf); 
$mime->setTXTBody($text);  
$mime->setHTMLBody($html);
$mime->addAttachment($file,'image/jpeg');
 
$headers = array ('From' => $from,
 
$headers .= $mime->headers($headers);
 
$mail = $smtp->send($to, $headers, $body);
 
'To' => $to,
'Subject' => $subject);
 
 
 
if (PEAR::isError($mail)) {
// else update attempts, last attempt
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"last_attempt = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql, $link);
 
//echo( $mail->getMessage() );
} else {
// if successful, update attempts, success, last attempt, date_sent
$sql = "UPDATE email_queue SET " .
"attempts = attempts+1, " .
"success = '1', " .
"last_attempt = now(), " .
"date_sent = now() " .
"WHERE id = '" . $queued_mail['id'] . "'";
mysql_query($sql, $link);
 
echo("Message successfully sent!");
}
} // end while (loop through records and sending emails)
} // no rows so quit
 
// release resources
mysql_free_result($result);
mysql_close($link);
?>