Jump to content
Larry Ullman's Book Forums

Chapter 12-Sending An Email With Attachment


Recommended Posts

Larry explained explicitly how to backup in chapter 10. I want to go further by sending an attachment of the backed up file. Has anyone done this already?

 

with the syntax $mime->addAttachment($file_name,$file_type); can someone tell me the $file_type?

 

Also, "Testing HTML Email with an Image" didn't work for me but "Sending HTML Email" worked when i maintained the factory function argument as "mail" although i was using smtp. Can someone explain that to me?

 

Thanks

Link to comment
Share on other sites

Thanks larry for your response. One more thing, how can i get around this

 

$mime->addAttachment("F:/backups/$db_name/{$table}_{$time}.sql.gz",plain/text);

 

i tried $file_name='F:/backups/$db_name/{$table}_{$time}.sql.gz'; and then

 

$mime->addAttachment($file_name,plain);

 

but never worked out probably due to the variables. Any help will be very much appreciated.

Link to comment
Share on other sites

I don't know exactly what you mean by "get around this", but I will say that this code:

$file_name='F:/backups/$db_name/{$table}_{$time}.sql.gz';

won't work because variables within single quotes aren't extrapolated.

Link to comment
Share on other sites

Thanks Larry for your response. I finally got around it using mysqldump and windows scheduler. my only problem using mysqldump is that all the records/rows come in a long string of characters making it difficult to read.

 

Is there a way to add a new line to each row in mysqldump? I have searched all over but not getting anywhere.

 

PHP 5.3.1

Mysql 5.1.41

Windows 2003 server OS

 

The following backup and email script could be of help to novices like me:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

 

/*

Quickly and easily backup your MySQL database and have the

tgz emailed to you.

 

You need PEAR installed with the Mail and Mail_Mime packages

installed. Read more about PEAR here: http://pear.php.net

 

 

*/

 

require_once('Mail.php');

require_once('Mail/mime.php');

 

// location of your temp directory

$tmpDir = "F:/backups/";

// username for MySQL

$user = "user";

// password for MySQl

$password = "password";

// database name to backup

$dbName = "database_name";

// the zip file emailed to you will have this prefixed

$prefix = "db_databasename_HO_";

 

// email settings...

$to = "to@gmail.com";

$from = "from@gmail.com";

$subject = "db - backup-HO";

$sqlFile = $tmpDir.$prefix.date('d_m_Y').".sql";

$attachment = $tmpDir.$prefix.date('d_m_Y').".sql";

 

$creatBackup = "F:/xampp/mysql/bin/mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;

$createZip = "tar cvzf $attachment $sqlFile";

exec($creatBackup);

exec($createZip);

 

 

//system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > 'F:/backups/$sqlFile'

//");

 

$headers = array('From' => $from, 'Subject' => $subject);

$textMessage = $attachment;

$htmlMessage = "Backing up of database";

 

$mime = new Mail_Mime("\n");

$mime->setTxtBody($textMessage);

$mime->setHtmlBody($htmlMessage);

$mime->addAttachment($attachment, 'text/plain');

$body = $mime->get();

$hdrs = $mime->headers($headers);

$mail = &Mail::factory('mail');

$mail->send($to, $hdrs, $body);

 

echo "Backup completed successfully and emailed";

 

unlink($sqlFile);

unlink($attachment);

 

?>

 

 

</body>

</html>

Link to comment
Share on other sites

 Share

×
×
  • Create New...