0

I'm making PHP script, that import some data from mysql database and send it via email in attachment. But there is one issue - in future there is possibility, that data will be sensitive. So I need to set password to attachment.

I'm using phpmailer method addStringAttachment.

Some code:

<?php
require 'PHPMailerAutoload.php';
$con = new mysqli("x", "x", "x", "x");


$utf1 = "SET NAMES utf8";
$utf2 = "SET character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8'";

$con->query($utf1);
$con->query($utf2);


$output ='';
$result = $con->query("select * from xxx WHERE DATE( entry_date ) BETWEEN ( DATE( CURDATE( ) - INTERVAL 3 DAY )) AND ( DATE( CURDATE( ) - INTERVAL 1 DAY ) )");
$columns_total = $con->field_count;


// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
    $heading = mysqli_fetch_field_direct($result, $i)->name;
    $output .= '"'.$heading.'";';
}

$output .="\n";

// Get Records from the table
while ($row = $result->fetch_array()) {
    for ($i = 0; $i < $columns_total; $i++) {
        $output .='"'.$row["$i"].'";';
    }
    $output .="\n";
}

$filename = "myFile.xls";
header('Content-Encoding: UTF-8');         

$bomoutput = "\xEF\xBB\xBF" . $output;

$con->close();

PHPMailer:

$mail = new PHPMailer;

[...]

$mail->From = 'xx';
$mail->FromName = 'xx';
$mail->addAddress('xx');     // Add a recipient

$mail->addStringAttachment($bomoutput, $filename);   

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$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';
}

I hade some issues with UTF-8 chars, so I have to use UTF8 BOM. Can you give me some advice/tip, how to set attachment secuirty level at minimum? It does'nt metter how it will be encrypted - but it has to be. Give me a rod, please.

MarthyM
  • 1,839
  • 2
  • 21
  • 23
Tomasz
  • 181
  • 12

1 Answers1

0

This doesn't really have anything to do with PHPMailer or MySQL - they will store or send whatever you like, and if you want to encrypt it, that's up to you.

PHP has several functions that will allow you to encrypt strings - but making it usable is bit more involved if you want to be sure the recipient will be able to decrypt it easily - zip files are probably the easiest solution.

This question has some good suggestions.

Community
  • 1
  • 1
Synchro
  • 35,538
  • 15
  • 81
  • 104