简体   繁体   English

发送带有从数据库中获取数据的附加 php 文件的邮件

[英]Sending mails with attach php file which fetch data from database

when am trying to send multiple mails with the php file attachment which fetch the data from database, after sent successfully mails I am getting blank link in invite.php file when it reach in gmail, that means after sent the mail php file not fetching the data from database so it's not showing any link but all other contents are showing well and in localhost php link showing correct当我尝试发送带有从数据库中获取数据的 php 文件附件的多封邮件时,在成功发送邮件后,当它到达 gmail 时,我在 invite.php 文件中得到空白链接,这意味着发送邮件 php 文件后没有获取来自数据库的数据,所以它没有显示任何链接,但所有其他内容都显示良好,并且在 localhost php 链接中显示正确

This is my code这是我的代码

index.php索引.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

<?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>
?>

Have you any idea?你有什么想法吗?

file_get_contents() will only get contents of file (surprise :) ). file_get_contents()只会获取文件的内容(惊喜 :) )。 To execute it you have to use include or require and catch results with ob_*() .要执行它,您必须使用includerequire并使用ob_*()捕获结果。

I think it'll be better to create function which will return that links as string to avoid using output buffering.我认为创建将链接作为字符串返回以避免使用输出缓冲的函数会更好。

About output buffering you can read here: http://php.net/manual/en/book.outcontrol.php关于输出缓冲,您可以在这里阅读: http : //php.net/manual/en/book.outcontrol.php

I found one solution.我找到了一种解决方案。 Try this试试这个

<?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)

?>

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

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