Shubham Verma
Shubham Verma

Reputation: 39

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

This is my code

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 = '[email protected]';
$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>
?>

Have you any idea?

Upvotes: 0

Views: 1345

Answers (2)

Solomon Suraj
Solomon Suraj

Reputation: 1314

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 = "[email protected]";
$mailto = "[email protected]";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\r\n\r\Solomon";
$replyto = "[email protected]";
$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)

?>

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

file_get_contents() will only get contents of file (surprise :) ). To execute it you have to use include or require and catch results with 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

Upvotes: 3

Related Questions