Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数据库生成excel并用php将其发送到mail_Php_Excel_Email_Header - Fatal编程技术网

从数据库生成excel并用php将其发送到mail

从数据库生成excel并用php将其发送到mail,php,excel,email,header,Php,Excel,Email,Header,我已经创建了一个excel文件,我想用邮件发送它。谁能告诉我怎么做。 邮件正在发送,但excel文件没有发送。我正在php中使用mysql服务器。 这是我的密码 <?php session_start(); include ('../db_connect.php'); $user = $_SESSION['album_id']; header("Content-type: application/vnd.ms-excel"); header("Content-Disposition:

我已经创建了一个excel文件,我想用邮件发送它。谁能告诉我怎么做。 邮件正在发送,但excel文件没有发送。我正在php中使用mysql服务器。 这是我的密码

<?php
session_start();
 include ('../db_connect.php');

$user = $_SESSION['album_id'];
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=selected_image.xls");

 echo "<table>";
echo "<tr>";
echo "<td>";
echo "Imgae Name";
echo "</td>";
echo "<td>";
echo "Image Comment";
echo "</td>";
echo "</tr>";

$select_all = "select * from select_album where user_inf_id = '".$user."' and status = '1'";
$query_All = mysql_query($select_all) or die (mysql_error());
while ($row_all = mysql_fetch_assoc($query_All))
{
echo "<tr>";
echo "<td>";
echo $row_all['user_image'];
echo "</td>";
echo "<td>";
echo $row_all['user_comment'];
echo "</td>";
echo "</tr>";
 }
echo "</table>";

                $to         =   '2606ankit@gmail.com';  //put email address on which mail send
               $subject     =   "TEST SUBJECT";                 //Put subject of mail here
               $from        =   "webmaster@example.com";        //put email address from 
               //email body start
               $body        =   "Selected image mail";

               // Always set content-type when sending HTML email
                $headers = "MIME-Version: 1.0" . "\r\n";
                $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
                $headers .=  header("Content-type: application/vnd.ms-excel");
                $headers .=  header("Content-Disposition: attachment;Filename=selected_image.xls");
                // More headers
                $headers .= 'From: '.$from. "\r\n";

                //if you need to send cc mail then uncomment below line and change email address
                //$headers .= 'Cc: myboss@example.com' . "\r\n";

                mail($to,$subject,$body,$headers);


?>

我已经创建了一个excel文件,我想用邮件发送它。谁能告诉我怎么做。
邮件正在发送,但excel文件没有发送。我正在使用php中的mysql服务器。

如果您想发送附件,我强烈建议您使用。这绝对是更容易的选择。附加某些内容实际上与以下内容一样简单:

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();
要实现它,您需要做的就是

  • 下载它
  • 将整个“PHPMailer master”文件夹移动到项目中的某个位置,然后
  • 将其包含在您想要的php文件中:要求“Path/To/this/file/phpmailerautoad.php” 我使用它发送任何电子邮件,但当你想发送附件时,它特别有用。它确实简化了事情

    编辑


    下载中有许多示例,但这里有一个更完整的从Gmail帐户发送的示例:

    $mail = new PHPMailer;
    
    $mail->isSMTP();                                      // Set mailer to use SMTP
    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'gmailaccountusername@gmail.com';                 // SMTP username
    $mail->Password = 'PASSWORD';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
    
    $mail->From = 'example@gmail.com';
    $mail->FromName = 'YOUR NAME';
    $mail->addAddress('recipient@gmail.com', 'RECIPIENT NAME');
    $mail->addReplyTo('replyemail@gmail.com', 'REPLY NAME');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    
    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->addAttachment('/File/Path/To/Attachment.png');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  // Set email format to HTML
    
    // Send the email
    
    $mail->Subject = 'EMAIL SUBJECT';
    $mail->Body    = 'BODY OF EMAIL WHICH CAN INCLUDE HTML';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    

    请告诉我另一个简单的方法下载中有很多例子,你可以参考。我还在上面添加了一个编辑,详细介绍了一个更完整的实现。但是,您必须做一些工作,因为我对您的电子邮件提供商(SMTP主机、端口等)一无所知。