我知道有很多類似於我的問題,是的,我看過其中的一些,他們都沒有回答我的問題。電子郵件SQL附件問題
基本上我爲客戶端創建了一個腳本,它創建了數據庫的轉儲並通過電子郵件發送給他們,我將發佈下面的代碼,現在通過腳本生成數據庫信息並且沒有創建物理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} <[email protected]>'; # 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 protected], [email protected]'; #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);
?>
問題:當電子郵件被髮送附件和郵件內容不部落郵件客戶端或Hotmail顯示在所有,但它在Gmail中正常工作。我猜這是郵件標題的問題,但我無法看到任何錯誤。
任何人都可以看到電子郵件標題的任何問題?如果是的話,你可以告訴我如何解決這個問題?
一如既往,非常感謝幫助。
我還應該注意,它在Gmail中顯示正常,但在Hotmail或Horde中不顯示。我還沒有嘗試任何其他郵件客戶端。 – RobFos
作爲一個旁註,電子郵件不是通信的最安全的手段,發送你的數據庫與我看起來很危險 –
這不是一個解決方案,但是當我看你的代碼,我真的不得不建議你看看[ ezMail](http://ezcomponents.org/docs/tutorials/Mail),它可以輕鬆快速地處理通過PHP發送郵件的所有問題。沒有必要重新發明輪子。 –