Jump to content

Attempting to attach a file to the mail() function but no luck with header vars


Recommended Posts

I'm using a script that I found on-line and have modified it a little but am running in to an issue here.

 

Objective:  To create a DB backup, attached to an email and then send the email to a gmail account.  This will be called via a cron job nightly.

 

For now here is the code that I have..

<?php

$string = `mysqldump –user=user_name –password=pass db_name | gzip -9`;

// Email the mysqldump output as an attachment.
// Create an attachment filename that is datestamped.
$filename = "db_backup_" . date('Y-m-d') . ".sql.gz";

// Designate email addresses, names, etc.
$mailto = "[email protected]";
$from_name = "DB Backup";
$from_mail = "[email protected]";
$replyto= "[email protected]";
$subject = "Daily Database Backup";
$message = "DO NOT DELETE!!!";
// This next line takes our mysqldump output and encodes it
// for our use an as email attachment.
$content = chunk_split(base64_encode($string));
// We need a unique identifier for the attachment boundary
// within the email.
$uid = md5(uniqid(time()));
// Build the email header.
$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "–" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "–" . $uid . "\r\n";
// We need to use a valid Content-Type entry here for the attachment.
// You can see a list at http://en.wikipedia.org/wiki/Internet_media_type
$header .= "Content-Type: application/x-gzip; name=\"" . $filename . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "–" . $uid. "–";

// OK, let’s email the attachment and exit.
mail($mailto, $subject, "", $header);
exit(0);
?>

 

The Issue:  When I receive the email at the gmail account it contains no attachment and the body of the email looks like this..

 

This is a multi-part message in MIME format.

Ğ837ab75f0210edc1611ce3d30afb6173

Content-type:text/plain; charset=iso-8859-1

Content-Transfer-Encoding: 7bit

 

DO NOT DELETE!!!

 

Ğ837ab75f0210edc1611ce3d30afb6173

Content-Type: application/x-gzip; name="db_backup_2011-07-11.sql.gz"

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="db_backup_2011-07-11.sql.gz"

 

H4sIAHpBG04CAwMAAAAAAAAAAAA=

 

 

Ğ837ab75f0210edc1611ce3d30afb6173Ğ

 

Do you see anything that would cause this to happen?  I figured this line had the issue $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n"; but that did not seem to do the trick.  Any help would be greatly appreciated.

Cunnoodle,

You can use cron sendmail to accomplish this without the php part... look here: http://www.linuxquestions.org/questions/linux-newbie-8/send-email-txt-attachment-using-cron-job-43492/

or use the php mail function, just little more complicated... look here : http://www.dreamincode.net/forums/topic/24215-php-mail-attachment/

then use the cron to execute the task...

 

ps. i didn't test this... I use windows sendmail, to do the email with attachment... here: http://www.makeuseof.com/tag/send-automated-emails-save-time-sendemail-windows-task-scheduler/

but i'm guessing you're using linux  :'(

goodluck!!!

Yeah, Its a linux server on a shared hosting environment so I really don't have much options in terms of configuration of the server itself.  Anyone else have any suggestions on how to make this work better?  All I'm looking for is a daily db backup to be emailed out.  Everything I search for on google suggests installing Pear and using mail.php... unfortunately I don't have that option.

cunoodle,

I found this code online. It will send out email with attachment and I tested it, and worked fine!!! You might run into a path issue if you don't give the correct path which will result in an empty file attachment... so just watch out for that. Here's the code...

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) 
{
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}

$my_file = "something.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your paht here/";
$my_name = "your name";
$my_mail = "[email protected]";
$my_replyto = "[email protected]";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>

So just give the right path to your backup file...

that should do... Here's the source code: http://www.finalwebsites.com/forums/topic/php-e-mail-attachment-script

 

goodluck! :-*

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.