I've been searching all day and have not found the code that works. I'm trying, after submitting form,
This is a scaled down version
<?php
$to = "email@email.com";
if(isset($_POST['submit']))
{
// VALIDATION
if(empty($_POST['address']))
{
"First Name Required";
}
if(empty($_POST['email']))
{
"Last Name Required";
}
if(empty($error))
{
$subject = 'The Form';
$headers = "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html;
" ;
$headers .= "From: from@theemailaddress.com
"."Reply-to: {$_POST['email']}
";
$msg .="<html>
<head></head>
<body>
<table>
<tr><td>
<table>
<tr><td>This is the email sent.</td></tr>
</table>
</body>
</html>";
include('con.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thetable");
$email= mysqli_real_escape_string($con, $_POST['email']);
$address= mysqli_real_escape_string($con, $_POST['address']);
$sql = "SELECT * FROM thetable WHERE `email` = '{$email}' OR `address` = '{$address}'";
$result = mysqli_query($con,$sql);
if(($result->num_rows)>= 1)
{
$theerror = "You exist";
}
else
{
$sql="INSERT INTO thetable(email, address) VALUES ('$_POST[email]','$_POST[address]'";
$success = "Sent ... Insert it!!!";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
//The Attachment
$cr = "
";
$data = "Email" . ',' . "address" . ',' . $cr;
$data .= "$email" . ',' . "$address" . $cr;
$fp = fopen('diploma_apprenticeship_form_sub.csv','a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => 'diploma_apprenticeship_form_sub.csv',
'type' => 'application/vnd.ms-excel'
);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Add the headers for a file attachment
$headers = "MIME-Version: 1.0
" .
"From: {$from}
" .
"Content-Type: multipart/mixed;
" .
" boundary=\"{$mime_boundary}\"";
//Add a multipart boundary above the plain message
$msg= "This is a multi-part message in MIME format.
" .
"--{$mime_boundary}
" .
"Content-Type: text/html; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit
" .
$text . "
";
//Add sttachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$msg.= "--{$mime_boundary}
" .
"Content-Type: {$type};
" .
" name=\"{$name}\"
" .
"Content-Transfer-Encoding: base64
" .
$data . "
" ;
}
$msg.= "--{$mime_boundary}--
";
$result = @mail($to, $subject, $msg, $headers);
}
mysqli_close($con);
{
}
}
}
?>
Try this,
$f = fopen('path to file', 'w'); //path such as __DIR__./file.csv'; write mode.
fputcsv( $f, $data);
//data is an array of data ie array('one', 'two'); is one,two in the file ~ make a loop
//around this and write as many lines as you need, like array(header, header1); then
//array(data, data1) etc...
fclose($f); //close the file when done
http://phpmailer.worxware.com/
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = 'from@example.com';
$mail->addAddress('joe@example.net', 'Joe User');
$mail->addAttachment( __DIR__.'/file.csv');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
unlink( __DIR__.'/file.csv' ); //remove the file
Oh and get rid of all that header stuff, let php mailer do its job, which as you mentioned is not to validate your data. Work up a process flow.
Input Validate Calculate ( assign values ) Output ( send your email ) Clean up ( remove any files, etc.. )
etc..
AS An update
$msg .="<html>
<head></head>
<body>
<table>
<tr><td>
<table>
<tr><td>This is the email sent.</td></tr>
</table>
</body>
</html>";
... and then latter
$msg= "This is a multi-part message in MIME format.
" .
"--{$mime_boundary}
" .
"Content-Type: text/html; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit
" .
$text . "
";
Also your likely to get a warning for the first message as you are not defining the variable before using the concant on it.
$msg .= 'something';
Should be
$msg = 'something';
Or
$msg = '';
$msg .= 'something';