我创建了一个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服务器。
答案 0 :(得分:0)
如果您想发送附件,我强烈建议您使用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();
要实现它,您需要做的就是
我用它来发送任何电子邮件,但是当你想发送附件时它特别有用。它确实简化了事情。
修改强>
下载中有很多示例,但以下是从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';
}