我知道有很多类似于我的问题,是的,我看过其中的一些问题,似乎没有人回答我的问题。
基本上我已经为客户端创建了一个脚本,它创建了一个数据库转储并通过电子邮件发送给他们,我将发布下面的代码,现在通过脚本生成数据库信息,并且没有创建物理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);
?>
问题:当发送电子邮件时,附件和邮件内容根本不显示在部落邮件客户端或hotmail中,但它在gmail中运行正常。我猜这是邮件标题的问题,但我无法看到任何错误。
任何人都可以看到电子邮件标题有任何问题吗?如果是这样,你能告诉我如何解决这个问题吗?
一如既往,非常感谢帮助。
答案 0 :(得分:1)
圣洁的莫莉!
使用mime对象创建格式正确的电子邮件并非易事。我已经破解了元邮件代码但不会梦想编写我自己的PHP代码来处理它。我没有本地实现的mime功能邮件客户端,我可以从PHP(metamail,mutt)调用,我会使用一个免费的库来处理它(phpmailer,swiftmailer是明显的候选人)。
我倾向于避免的另一件事是在PHP脚本中创建内存中数据库备份的表示。
我不会做的其他事情是:
我建议再次启动 - 设置一个cron作业将备份解压缩到本地文件,然后编写一个PHP来访问该文件 - 并通过电子邮件向用户发送一个指向他们可以下载备份的链接。
答案 1 :(得分:0)
你应该把身体放在体内。它可能是一个MIME消息,因此包含标题,但它在技术上仍然是消息的主体,它应该在哪里。
此外,你有一些不必要的\r\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";
$mail_header .= 'MIME-Version: 1.0' . "\r\n";
// The body (multipart message) starts here:
$mail_body = 'This is a multi-part message in MIME format' . "\r\n";
// HTML content of the email
$mail_body .= "$new_boundary\r\n";
$mail_body .= 'Content-Type: text/html; charset="iso-8859-1"' . "\r\n";
$mail_body .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
$mail_body .= $E_content . "\r\n";
// This part of the body is for the attachment and includes the contents of the file, the files name and other information.
$mail_body .= "$new_boundary\r\n";
$mail_body .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "\r\n";
$mail_body .= 'Content-Transfer-Encoding: base64' . "\r\n";
$mail_body .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "\r\n\r\n";
$mail_body .= $data_split . "\r\n";
//This is needed to stop any rendering errors by email clients and signifies the end of the email's body.
$mail_body .= "$new_boundary--";
//This mails out all of the above.
mail($E_recipients, $E_subject, $mail_body, $mail_header);