使用附加php文件发送邮件,该文件从数据库中获取数据

时间:2013-10-08 15:25:02

标签: php mysql email

当我试图发送带有从数据库中获取数据的php文件附件的多封邮件时,发送成功邮件后,当它到达gmail时,我在invite.php文件中获得空白链接,这意味着在发送邮件php文件后没有从数据库中获取数据,所以它没有显示任何链接,但所有其他内容都显示良好,并在localhost php链接显示正确

这是我的代码

的index.php

<?php 
if(!empty($_POST['invite'])) {
foreach($_POST['invite'] as $check) {
    }
$import_emails =  implode($_POST['invite'], ',');
$imp_eml = explode(',', $import_emails); 

foreach ($imp_eml as $addr)
{
$mail->AddBCC($addr);
}  
$mail = new PHPMailer();
$body = file_get_contents('invite.php');
$body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->From = 'info@myweb.com';
$mail->FromName = 'Someone';
$mail->Host = '********';
$mail->SMTPAuth = true; 
$mail->Username = '*******';
$mail->Password = '*******';

if($mail->Send())
{
 echo "success";
} else {
 echo "failure";
} 
}
?>

invite.php

<?php
include("connect.php")
$customerid = $_REQUEST['customerId']; //comming from another page

$query = mysql_query("select * from  table where customerId = '$customerid'");
 while($fetch=mysql_fetch_array($query)) {
 $prjid = $fetch['projectId'];
 }
?>
Vote for my projects <?php echo '<a href="http://www.mywebsite.com/project-details/'.$c_id.'/'.$p_id.'/">here</a>'; ?>.<br/>
<a href="http://www.mywebsite.com/project-details/<?php echo $c_id.'/'.$p_id.'/'; ?>"><?php echo 'http://www.mywebsite.com/project-details/'.$c_id.'/'.$p_id.'/'; ?></a>
?>

你有什么想法吗?

2 个答案:

答案 0 :(得分:3)

file_get_contents()只会获取文件内容(惊讶:))。要执行它,您必须使用includerequire并使用ob_*()捕获结果。

我认为最好创建一个将链接作为字符串返回的函数,以避免使用输出缓冲。

关于输出缓冲,您可以在此处阅读:http://php.net/manual/en/book.outcontrol.php

答案 1 :(得分:0)

我找到了一个解决方案。试试这个

<?php
$con = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$con)
{
    die("error" . mysqli_connect_error());
}

error_reporting(E_ERROR);
$filename = "email_data";
$sql = mysqli_query($con, "SELECT * FROM email_data order by id asc limit 0,100");
$row = mysqli_fetch_assoc($sql);
$filename2='datas/'.$filename.'.csv';
$fp = fopen($filename2, "w");
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $name);$comma = ",";}
$seperator .= "\n";
$seperator;
fputs($fp, $seperator);
mysqli_data_seek($sql, 0);
while ($row = mysqli_fetch_assoc($sql))
{
    $seperator = "";
    $comma = "";
    foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $value);$comma = ",";}
    $seperator .= "\n";
    fputs($fp, $seperator);
}

fclose($fp);

$my_file = $filename2;
$path = "datas/";
$from_name = "solomon";
$from_mail = "pss@gmail.com";
$mailto = "pssworkcse@gmail.com";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\r\n\r\Solomon";
$replyto = "pssworkcse@gmail.com";
$file = $my_file;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: application/octet-stream; name=\"" . $filename2 . "\"\r\n"; 
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename2 . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
mail($mailto, $subject, "", $header)

?>