繁体   English   中英

从数据库中制作出卓越的Excel,并发送到php中的邮件

[英]make excel from database and send it to mail in php

我已经创建了一个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服务器。

如果要发送附件,强烈建议您使用PHPMailer 绝对是更简单的选择。 附加东西真的很容易:

$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();

要实现它,您需要做的就是

  1. 在这里下载;
  2. 将整个“ PHPMailer-master”文件夹移动到项目中的某个位置,然后;
  3. 包括在您想要的php文件中:require'Path / To / This / File / PHPMailerAutoload.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';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM