<?php
include 'include/function.php';
//$random_hash = md5(time());
$User=new User();
session_start();
$id=$_POST['id'];
//$date=$_POST['date'];
$expected_date=$_POST["expected_date"];
$comname=$_POST['comname'];
$type=$_POST["type"];
$name=$_POST["name"];
$mime=$_POST["mime"];
$size=$_POST["size"];
$file1=$_POST["path"];
//$comname=$_POST["comname"];
$remark=$_POST["remark"];
$other_detail=$_POST['other_detail'];
$remark=$_POST["remark"];
//$last_change_time=$row['last_change_time'];
$email1=$_POST['email1'];
$email2=$_POST['email2'];
$email3=$_POST['email3'];
$email4=$_POST['email4'];
$email5=$_POST['email5'];
$email6=$_POST['email6'];
$username=$_SESSION["username"];
//$username=$row['username'];
$sql=mysql_query("update depository set expected_date='$expected_date',comname='$comname',last_change_username='$username',type='$type',name='$name',mime='$mime',size='$size',path='$file1',other_detail='$other_detail',remark='$remark',email1='$email1',email2='$email2',email3='$email3',email4='$email4',email5='$email5',email6='$email6'where id='$id'");
$htmlbody = " Message successfully send ok";
//$to .= msh#solnfinite.com;
//Recipient Email Address muktidar#gmail.com
$to = $email1. ', ';
$to .= $email3. ', ';
$to .= $email2. ', ';
$to .= "mukeshbpatidar#gmail.com";
$subject = 'Depositiory Detail From Solution Infinite'; //Email Subject
$headers = "From: ticketing#soite.com\r\nReply-To: mukesh#solite.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($file1))); // Set your file path here
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message = "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/pdf; name=\"$name\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
?>
When I send email through windows than send properly but when I run script on linux than attachment mail send in plain text.
Email sent in full text shown in below:
PHP-mixed 83d06f048e070e4cfc0684d0e98f71db
Content-Type: application/jpg; name="tkt.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAMABVYDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
It is because of situations like this (cross-platform compatibility) that the PHP constant PHP_EOL exists. Do this:
<?php
....
$eol = PHP_EOL;
$headers = "From: ticketing#soite.com".$eol."Reply-To: mukesh#solite.com";
$random_hash = md5(date('r', time()));
$headers .= $eol."Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($file1))); // Set your file path here
//define the body of the message.
$message = "--PHP-mixed-$random_hash".$eol."Content-Type: text/html; charset=\"iso-8859-1\"".$eol."Content-Transfer-Encoding: 7bit".$eol.$eol;
//Insert the html message.
$message .= $htmlbody.$eol.$eol."--PHP-mixed-$random_hash".$eol;
//include attachment
$message .= "Content-Type: application/pdf; name=\"$name\"".$eol."Content-Transfer-Encoding: base64".$eol."Content-Disposition: attachment; filename=\"".$name."\"".$eol;
$message .= $attachment;
$message .= $eol."--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
?>
EDIT:
Based on new information in your comment, and another look at the code, I wonder why windows ever delivered the message as html. You specified the content-type as text/plain here
$message = "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n"
You should change that to simply
$message = "--PHP-alt-$random_hash".$eol."Content-type:text/html; charset=iso-8859-1".$eol."Content-Transfer-Encoding: 7bit".$eol.$eol;
This should most likely resolve your problem.
EDIT 2
Check changes I made to the main answer...
Use Linux style newline characters: \n instead of \r\n, or, use PHP_EOL constant.
Use PHP_EOL constant. It substitutes end of line based on platform
specific.
Note: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.Alternatives to this function include: mysqli_query() or PDO::query()
You basically have two options to achieve this:
1.Using PDO (for any supported database driver):
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute(array('name' => $name));
foreach ($stmt as $row) {
// do something with $row
}
2.Using MySQLi (for MySQL):
$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with $row
}
Please refer How can I prevent SQL-injection in PHP?
Related
I have the following code, which correctly sends an email with an attachment of the correct size, however the attachment comes in as 'noname' without an extension. If I rename the file manually after downloading it does not work. The file is an mp4 video.
<?php
$htmlbody = " Your Mail Contant Here.... You can use html tags here...";
$to = "blah#gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: test#mysite.com\r\nReply-To: test#mysite.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents('test.mp4')));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: video/vnd.uvvu.mp4
name=\"testing.mp4\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
?>
Just modify this line, everything else stays the same:
$filename= "myvideo.mp4";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: video/vnd.uvvu.mp4
name=\"testing.mp4\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
I have a multi-part email script, that takes a POSTed email address and sends a simple email to them in HTML and/or Plain Text. It displays correctly in Gmail and Outlook, but not eM (and doesn't even get through to a Communigate server). The code:
<?php
$email_address = addslashes($_POST['email_address']);
if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
header("Location: ./?error=invalid-email");
exit();
}
$subject_line = "This is a test multi-part email";
$boundary = uniqid();
$headers = "MIME-Version:1.0\r\n";
$headers .= "From: Maggie Multipart <web#ukipme.com>\r\n";
$headers .= "To: " . $email_address . "\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=utf-8\r\n\r\n";
$message .= "Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=utf-8\r\n\r\n";
$message .= "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>";
$message .= "\r\n\r\n--" . $boundary . "--";
mail("", $subject_line, $message, $headers);
header("Location: ./?success=email-sent");
exit();
// var_dump($_POST);
?>
The message is received in eM as follows:
Content-Type: text/plain;charset=utf-8
Hello,
This is a test email, the text/plain version.
Regards
Maggie Multipart
However, eM is set up to receive HTML emails (and does so frequently). Can someone please help me fix this problem? Am I missing any headers?
My general advice for creating emails: Don't do it yourself (with some string concat functions/operators anyway). My weapon of choice is swiftmailer but there are other feasible libraries on the web, too.
<?php
require_once('autoload.php'); // swiftmailer was installed via Composer
$message = Swift_Message::newInstance('This is a test multi-part email')
->setBody(
"Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart",
'text/plain',
'utf-8'
)
->addPart(
"<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>",
'text/html',
'utf-8'
)
->setFrom(array('...#...' => '...'))
->setTo(array('...#...' => '...'));
$transport = Swift_SmtpTransport::newInstance('MSERV', 25, 'tls')
->setUsername('...')
->setPassword('...');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
I had reference this solution from other post and I tried modify the content to suit my requirement.
Here is the code that I implemented to send attachment file via mail using PHP.
$htmlbody = " Your Mail Contant Here.... You can use html tags here...";
$to = "foo#bar.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: foo#bar.com\r\nReply-To: foo#bar.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$path = $_FILES["cv"]["name"];
$attachment = chunk_split(base64_encode(file_get_contents($path)));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip;
name=\"$path\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
I able to get the attachment file but I cant open it.
It show me the error of "The application you chose ("") could not be found. Check the file name or choose another application."
Can anyone correct my error..?
It seems that the problem relies in the file name after download. The server did the job and sent the email and this is rather a client side problem.
Check the filename extension and make sure it is the same as the side that sent it, If not make sure u correct your PHP code naming function.
Make sure the application that reads that filetype is available on your PC.
Try to open the file on a different computer.
How do i send a mail in PHP with attachments?
I want to use pure php and not any libraries...
Here's what I already have tried:
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Try this:
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Reference: http://webcheatsheet.com/PHP/send_email_text_html_attachment.php
You can try the following, but first you will need to make changes to these:
$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;
Which is within the code below: (pre-tested)
<?php
// Set who this goes to:
$to = array('Your Name','email#example.com');
// Give the message a subject:
$subject = 'Some subject';
// Set who this message is from:
$from = array("My Company", "email#example.com");
// Create the header information:
$random_hash = md5(date('r', time()));
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n";
$headers .= 'To: '.$to[0].' <'.$to[1].'>' . "\r\n";
$headers .= 'From: '.$from[0].' <'.$from[1].'>' . "\r\n";
// Build the message (can have HTML in it) message:
$message = 'This is an <b>HTML message</b> it comes with an attachment!'."\n\n".
// Do not edit this part of the message:
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// The image that you would like to send (filename only):
$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;
$fileatt_type = "application/octet-stream"; // File Type
// Filename that will be used for the file as the attachment
$fileatt_name = $name;
// Read the file attachment:
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Create sendable information
$data = chunk_split(base64_encode($data));
// Finalize the message with attachment
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
// Send the message:
mail($to[1], $subject, $message, $headers);
echo "Email sent";
?>
You need to make use of base64 encoding inorder to attach a file. So here goes a simpler code.
<?php
$to = "someone#somewhere.com";
$subject = "Email with an Attachment with no Special libraries";
$random_hash = md5(date('r', time()));
$headers = "From: someid#yoursite.com\r\nReply-To: noreply#so.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("pics.zip")));
$output = "somecontent";
echo mail($to, $subject, $output, $headers);
?>
$to = 'my#email.ca';
$subject = 'Receipt';
$repEmail = 'rep#sales.ca';
$fileName = 'receipt.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Sender <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "This is a MIME encoded message.".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
$action = 'action=Receipt%20Sent';
header('Location: ../index.php?'.$action);
}
else {
$action = 'action=Send%20Failed';
header('Location: ../index.php?'.$action);
}
I have been using TCPDF for a short amount of time now to generate PDF files from forms. It works quite well and that part of the PHP has not changed. Now I want to send those PDF files to my email account.
The emailing is actually working with this coding and attaching a PDF. The issue is that it is simply a blank PDF at rough 100 bytes in size. Which of course is not a valid PDF nor does it have anything to do with the responses from the form.
I am really not familiar with the attaching of files to an email in PHP and any help resolving this issue would be greatly appreciated.
Update
Since it seems like several people are looking at this still I will post my current solution. It involves downloading PHPMailer as suggested below. I have started at the output line for TCPDF.
$attachment = $makepdf->Output('filename.pdf', 'S');
SENDmail($attachment);
function SENDmail($pdf) {
require_once('phpmailer/class.phpmailer.php');
$mailer = new PHPMailer();
$mailer->AddReplyTo('reply#to.ca', 'Reply To');
$mailer->SetFrom('sent#from.ca', 'Sent From');
$mailer->AddReplyTo('reply#to.ca', 'Reply To');
$mailer->AddAddress('send#to.ca', 'Send To');
$mailer->Subject = 'Message with PDF';
$mailer->AltBody = "To view the message, please use an HTML compatible email viewer";
$mailer->MsgHTML('<p>Message contents</p>'));
if ($pdf) {$mailer->AddStringAttachment($pdf, 'filename.pdf');}
$mailer->Send();
}
You have two choices. You can save the PDF to a file and attach the file or else output it as a string. I find the string output is preferable:
$pdfString = $pdf->Output('dummy.pdf', 'S');
The file name is ignored since it just returns the encoded string. Now you can include the string in your email. I prefer to use PHPMailer when working with attachments like this. Use the AddStringAttachment method of PHPMailer to accomplish this:
$mailer->AddStringAttachment($pdfString, 'some_filename.pdf');
I tried several alternatives. Only way that worked was when I saved the PDF to a folder and then email it.
$pdf->Output("folder/filename.pdf", "F"); //save the pdf to a folder
require_once('phpmailer/class.phpmailer.php'); //where your phpmailer folder is
$mail = new PHPMailer();
$mail->From = "email.com";
$mail->FromName = "Your name";
$mail->AddAddress("email#yahoo.com");
$mail->AddReplyTo("email#gmail.com", "Your name");
$mail->AddAttachment("folder/filename.pdf"); // attach pdf that was saved in a folder
$mail->Subject = "Email Subject";
$mail->Body = "Email Body";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent";
}
echo 'sent email and attachment';