1

I am storing a pdf or text file in mysql blob object, and I am trying to send that as a attachment in the mail using php mailer.

I am sending the mail like .

$mail->AddAddress($pr_email);
$mail->Subject = "Meeting Invitation -$meeting_name";

$mail->AddStringAttachment($data);
$mail->IsHTML(true);
$mail->Body =$message;

if(!$mail->Send())
{
    $message= "Error sending: " . $mail->ErrorInfo;
    header("Location:userprofile.php?Message={$message}");
}

Where $data came from

$tmpName  = $_FILES['image']['tmp_name'];
$fp     = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$query = "UPDATE demo_meeting SET attachment='$data' where meetingID='$mtngid'";
$results = mysql_query($query);

Now I am receiving the mail as an attachment but content is not there, it is only showing 0K.

Please tell me what I might be doing wrong.

Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
user1667633
  • 4,449
  • 2
  • 16
  • 21
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Anthony Hatzopoulos Nov 02 '12 at 16:02

1 Answers1

1

As I can see here in StackOverflow, you have ot pass additional parameters to AddStringAttachment: the mime type and the file name.

$filename="test.png";
$encoding = "base64"; 
$type = "image/png";
$mail->AddStringAttachment(base64_decode($data), $filename, $encoding, $type);

Also, NEVER use addslashes to "secure" a mysql field, you have to use mysql_real_escape_string instead (or, use PDO_MySQL driver which won't be marked obsolete anytime soon)

Community
  • 1
  • 1
haltabush
  • 4,508
  • 2
  • 24
  • 41