【发布时间】:2011-12-05 05:26:10
【问题描述】:
我知道有很多与我类似的问题,是的,我浏览了其中一些,但似乎没有一个回答我的问题。
基本上我已经为客户创建了一个脚本,该脚本创建了他们的数据库转储并将其通过电子邮件发送给他们,我将在下面发布代码,现在数据库信息是通过脚本生成的,并且没有创建物理 sql 文件服务器,而不是将其全部添加到一个变量中,然后使用该变量将数据库信息附加到电子邮件标题。
另外请注意,我以前使用过电子邮件附件的代码,它工作正常,但在那种情况下,文件确实存在,所以我开始认为这是问题所在。
<?php
//Script configuration section.
$D_hostname = 'localhost'; # Database hostname.
$D_username = 'username'; # Database username.
$D_password = 'password'; # Database password.
$D_database = 'database'; # Database name.
$E_from = '{BLOG BACKUP} <backup@mywebsite.com>'; # From header.
$E_subject = '[' . date('d-m-Y') . ']: This weeks database backup.'; # Email subject.
$E_content = 'This weeks database backup has been created, please find it attached to this email. Remember to save a copy and retain this email too!<br />'; #Email content.
$E_filename = date('d-m-Y') . '_blog_backup.sql'; #Attachment filename.
$E_filetype = 'application/octet-stream'; # Attachment type.
$E_recipients = 'email@mywebsite.com, email@myclient.com'; #Email recipients.
//Connect to the database.
$connection = mysql_connect($D_hostname, $D_username, $D_password);
//Select the database
mysql_select_db($D_database, $connection);
//Set up an empty array.
$tables = array();
//Get a list of all the databases tables.
$Q_show_tables = mysql_query('SHOW TABLES');
//Add each table to the array.
while($R_show_tables = mysql_fetch_row($Q_show_tables)){
$tables[] = $R_show_tables[0];
}
//Get all rows for each table and create a mySQL query.
foreach($tables as $table) {
//Get all rows from the table.
$Q_get_rows = mysql_query('SELECT * FROM '.$table);
$C_get_rows = mysql_num_fields($Q_get_rows);
//Add a drop clause for the table
$data_row .= 'DROP TABLE ' . $table . ';';
//Get create table info.
$Q_create_table = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
//Add the create table clause to the query.
$data_row.= "\n\n" . $Q_create_table[1] . ";\n\n";
//Now gather all of the rows.
for ($int_01 = 0; $int_01 < $C_get_rows; $int_01++) {
while($R_get_rows = mysql_fetch_row($Q_get_rows)) {
//Add the insert clause.
$data_row .= 'INSERT INTO ' . $table . ' VALUES (';
//For each field write out a row.
for($int_02 = 0; $int_02 < $C_get_rows; $int_02++) {
$R_get_rows[$int_02] = addslashes($R_get_rows[$int_02]);
$R_get_rows[$int_02] = ereg_replace("\n","\\n",$R_get_rows[$int_02]);
if (isset($R_get_rows[$int_02])) {
$data_row .= '"'.$R_get_rows[$int_02].'"' ;
} else {
$data_row .= '""';
}
if ($int_02 < ($num_fields-1)) {
$data_row .= ',';
}
}
$data_row .= ");\n";
}
}
$data_row .="\n\n\n";
}
//Split and encode the data.
$data_split = chunk_split(base64_encode($data_row));
//Create a unique boundary
$new_boundary = md5(time());
//This is your basic header information such as who the email is from and the date it was sent.
$mail_header .= 'From: ' . $E_from . "\r\n";
$mail_header .= 'Date: ' . date('r') . "\r\n";
//This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
$mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "\r\n\r\n";
$mail_header .= 'MIME-Version: 1.0' . "\r\n";
$mail_header .= 'This is a multi-part message in MIME format' . "\r\n";
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
$mail_header .= $E_content . "\r\n\r\n";
//This part of the header is for the attachment and includes the contents of the file, the files name and other information.
$mail_header .= '--' . $new_boundary . "\r\n";
$mail_header .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "\r\n";
$mail_header .= 'Content-Transfer-Encoding: base64' . "\r\n";
$mail_header .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "\r\n\r\n";
$mail_header .= $data_split . "\r\n\r\n";
//This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
$mail_header .= '--' . $new_boundary . '--' . "\r\n";
//This mails out all of the above.
mail($E_recipients, $E_subject, '', $mail_header);
?>
问题: 发送邮件时,附件和邮件内容在horde mail客户端或hotmail中根本不显示,但在gmail中可以正常工作。我猜这是邮件标题的问题,但我看不到任何错误。
任何人都可以看到电子邮件标题的任何问题吗?如果是的话,你能告诉我如何解决这个问题吗?
一如既往,非常感谢您的帮助。
【问题讨论】:
-
我还应该注意,它在 Gmail 中显示正常,但在 Hotmail 或 Horde 中显示不正常。我还没有尝试过任何其他邮件客户端。
-
作为旁注,电子邮件不是最安全的沟通方式,发送您的数据库对我来说看起来很危险
-
这不是一个解决方案,但是当我查看您的代码时,我真的不得不建议您查看ezMail,它可以轻松快速地处理通过 PHP 发送邮件的所有问题。无需重新发明轮子。