php delay when sending mails from txt list - php

i have this script for sending mails from a emails.txt list and is working fine but is there a way to delay the sending of emails from the list?
Any help is appreciated,thanks!
This is the Php sender script:
if($_POST)
{
$recipient_email = '';
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$from_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING);
$bcc = filter_var($_POST["bcc"], FILTER_SANITIZE_STRING);
$reply_to_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$mesaj = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
//Get uploaded file data
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
$lista = $_FILES['lista']['tmp_name'];
if($file_error > 0)
{
die('Upload error or No files uploaded');
}
$handle = fopen($file_tmp_name,'rb');
// Now read the file content into a variable
$content = fread($file,filesize($file_tmp_name));
// close the file
fclose($handle);
// Now we need to encode it and split it into acceptable length lines
$encoded_content = chunk_split(base64_encode(file_get_contents($file_tmp_name)));
$uid = md5(date('r', time()));
//header
$headers = "From: ".$name." <".$from_email.">\r\n";
$headers .= "Bcc: $bcc\r\n";
$headers .= "Reply-To: ".$reply_to_email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$uid."\"\r\n\r\n";
$message = "--PHP-mixed-$uid\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$uid\"\r\n\r\n";
$message .= "--PHP-alt-$uid\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 .= $mesaj;
$message .="\r\n\r\n--PHP-alt-$uid--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$uid\r\n"."Content-Type: $file_type; name=\"$file_name\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $encoded_content;
$message .="Content-Transfer-Encoding: base64\r\n";
$message .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$message .= "/r/n--PHP-mixed-$uid--";
$list = fopen($lista, "r");
if ($list) {
while (($line = fgets($list)) !== false) {
if (!mail($line, $subject, $message, $headers))
{
echo "Eroare! Nu am putut trimite mailurile.";
}
else
{
echo "Mailurile au fost trimise!";
}
}
fclose($list);
}
}
Html form fields:
<form enctype="multipart/form-data" method="POST" action="">
<label>Name <input type="text" name="sender_name" /> </label>
</br><label>Mail <input type="email" name="sender_email" /> </label>
<label>Bcc <input type="text" name="bcc" /> </label>
</br><label>Subject <input type="text" name="subject" /> </label>
</br> <label>Message <textarea name="message"></textarea> </label>
</br><label>attachment <input type="file" name="my_file" /></label>
</br><label>Mail list <input type="file" name="lista" /></label>
</br> <label><input type="submit" name="button" value="Fire :)" /></label>
</form>

you can use the sleep() command to pause it
while (($line = fgets($list)) !== false) {
if (!mail($line, $subject, $message, $headers))
{
echo "Eroare! Nu am putut trimite mailurile.";
}
else
{
echo "Mailurile au fost trimise!";
}
sleep(3)
}
for example

Related

PHP file size validation for contact form with attachment

I've made contact form with php, and validation for name, email and message works great, but my file size validation doesn't work. When I try to upload file larger then 2 MB it's loading and collapse.
I've run out of ideas what to do. Can anyone help me?
Here is my php code:
<?php
if ($_POST["submit"]) {
if (!$_POST['name']){
$error="Unesite svoje ime.";
}
if (!$_POST['email']) {
$error.="Unesite e-mail adresu.";
}
if (!$_POST['message']) {
$error.="Unesite tekst poruke";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error.="Unesite validu e-mail adresu.";
}
if(!empty($_FILES['my-file']['name'])){
$file_name = $_FILES['my-file']['name'];
$temp_name = $_FILES['my-file']['tmp_name'];
$file_type = $_FILES['my-file']['type'];
if($_FILES['my-file']['size'] > 524288){
$error.="Maksimalna velicina fajla je 5 MB.";
} else {
$from = $_POST['email'];
$to = "nicolletta.dj#gmail.com";
$subject = "Poruka sa sajta";
$message = "Tekst poruke:".$_POST['message'];
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header = "MIME-Version: 1.0\r\n";
$header .= "From:".$from."\r\n";
$header .= "Reply-To: ".$_POST['email']."" . "\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";
//plain text part
$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";
//attachment part
$header .= "--".$uid."\r\n";
$header .= "Content-type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
if ($error) {
$result='<div class="alert alert-danger">'.$error.'</div>';
} else {
if (mail($to, $subject, "", $header)) {
$result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';}
else {
$result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}
} //if there is no error
}} else {if ($error) {
$result='<div class="alert alert-danger">'.$error.'</div>';
} else {
$header = "From:".$_POST['email']."\r\n";
$header = "Reply-To: ".$_POST['email']. "\n" ;
$body = "Tekst poruke:".$_POST['message'];
if (mail('nicolletta.dj#gmail.com', 'Poruka sa sajta', $body, $header)) {
$result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';}
else {
$result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}
}}//if files not empty
} //if is submited
?>
and my HTML code:
<div class="col-md-6 emailForm">
<?php echo $result; ?>
<form id="contact_body" method="post" action="index.php" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Ime i prezime:</label>
<input type="text" name="name" class="form-control" placeholder="Ime i prezime" data-required="true"/>
</div>
<div class="form-group">
<label for="email">E-mail:</label>
<input type="text" name="email" class="form-control" placeholder="E-mail" data-required="true"/>
</div>
<div class="form-group">
<label for="comment">Poruka:</label>
<textarea class="form-control" name="message" data-required="true"></textarea>
</div>
<div class="form-group">
<label class="btn btn-primary">
<input type="file" name="my-file" style="display:none;" onchange="$('#upload-file-info').html(this.files[0].name);">
Dodajte sliku
</label>
<span class='label label-info' id="upload-file-info"></span>
</div>
<div id="push" class="pushbutton"></div>
<input type="submit" name="submit" class="btn btn-lg" value="Poslati"/>
</form>
</div>
You can get error - because you have set lower value for php upload setting:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
If you reach this limits - the file is not uploaded and $_FILES[..][error]!=0.
And the second - 524288 is not 5MB but only 524kB
add a zero
if($_FILES['my-file']['size'] > 5242880)

PHP form sending email but not sending attachement

Hi I am receiving email with body but I cannot get attachment. I admit that this question has been asked before but I am bit new to this I am facing problem. I tried many other questions but I am not getting this. Here is my code
<?php
//$to_mail = "architects#palavin.com,t.lavin#palavin.com,12yorkcourt#gmail.com";
$to_mail = "myemail#gmail.com";
//$cc="paul#enhance.ie";
$mail_sent = 0;
if(isset($_POST['submit'])){
//echo "the form was submitted";
$name = trim(strip_tags($_POST['name']));
if($name == "")
$error = true;
$email = trim(strip_tags($_POST['email']));
if($email == "")
$error = true;
$phone = trim(strip_tags($_POST['phone']));
$address = trim(strip_tags($_POST['address']));
$description = trim(strip_tags($_POST['description']));
$select = trim(strip_tags($_POST['select']));
$address = trim(strip_tags($_POST['address']));
$location = trim(strip_tags($_POST['location']));
$availability = trim(strip_tags($_POST['availability']));
$brand = trim(strip_tags($_POST['brand']));
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode(($content)));
if($error != true){
$headers = 'From: "Contact Form" <no-reply#pintribe.com>'."\r\n";
//$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "";
$subject = "PinTribe Form";
$boundary = md5(time());
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/octet-stream; name=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$body .= $encoded_content ."\r\n";
$message = "New Contact message, received from: <br /> \n ";
$message .= "<b>Item Name</b> ".$name."<br /> \n";
$message .= "<b>Email</b> ".$email."<br /> \n";
$message .= "<b>Email</b> ".$brand."<br /> \n";
$message .= "<b>Contact Person</b> ".$phone."<br /> \n";
$message .= "<b>Address</b> ".$address."<br /> \n";
$message .= "<b>Category</b> ".$select."<br /> \n";
$message .= "<b>Description</b> ".$description."<br /> \n";
$message .= "<b>Location</b> ".$location."<br /> \n";
if(#mail($to_mail,$subject,$message,$headers))
{
echo "mail has been sent";
$mail_sent = 1;
}
else echo "mail not sent";
} else {
echo 'validation error';
}
}
?>
HTML
<form role="form" action="send_form_email.php" method="post" class="login-form" enctype="multipart/form-data">
<div class="form-group">
<span>Attach Photos</span>
<label class="btn btn-default btn-file fa fa-plus">
<input type="file" name="my_file">
</label>
<br>
<small>A maximum of 3 photos. Make sure Photos are bigger than 450 px X 850px</small>
</div>
<button type="submit" name="submit" class="btn">submit Form</button>
</form>
I can see everything means Body is been received but unable to receive Attached file

Form data not sent in mail while attachment is sent in PHP

I am tyring to have a contact form on my website having option for visitor to send attachment with their information. I tried but couldn't succeed. The problem i am having is that the file is sent to the email but the form data and email subject is not sent with the attachment.
HTML:
<form name="form1" enctype="multipart/form-data" method="post" action="do_send.php">
<label>
<input type="text" name="name" id="name" />
</label>
<label>
<input type="text" name="age" id="age" />
</label>
<label>
<input type="email" name="email" id="email" />
</label>
<label>
<input type="file" name="my_file" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>
do_send.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
if($_POST && isset($_FILES['my_file']))
{
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
# Mail headers should work with most clients (including thunderbird)
$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From:".$email."\r\n";
$headers .= "Subject:".$subject."\r\n";
$headers .= "Reply-To: ".$email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $MESSAGE_BODY."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
if ($_POST["email"]!='') {
$ToEmail = 'info#mywebiste.com';
$EmailSubject = 'Website contact form';
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Age: ".$_POST["age"]."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $headers) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
return true;
} else{
echo "<script> alert('Temporary problem, try again!');
window.location='index.html'</script>";
}
}
?>
Kindly point out where i am doing mistake. I am in learning stage please help me solving this issue. Please guide me where to do amendment in the do_send.php so that the form data and email subject is also sent with the attachment.
<?php
$nom = $_POST['lastName'];
$prenom = $_POST['firstName'];
$from_email = $_POST['email']; //sender email
if($_POST && isset($_FILES['my_file']))
{
$recipient_email = 'youremail#exemple.com'; //recipient email
$subject = 'Inscription Etudiant'; //subject of email
$message = 'This is body of the message'; //message body
$message = 'Nom: '.$nom."\n";
$message .= 'Prenom: '.$prenom."\n";
$message .= 'Email: '.$from_email;
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("YADIStudio");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.php';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to youremail#exemple.com');
window.location = 'index.php';
</script>
<?php
}
}
?>
<--! YADI Studio Agency Copyright 2015 -->

Website form - Multiple file upload [duplicate]

This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 8 years ago.
So I have searched through a lot of questions similar to this but cannot seem to find a solution.
I have a contact form and I want it to be able to send multiple images. The code I have now has three input fields for pictures but right now I have only figured out how to get one of the pictures to upload and send. I am pretty sure I need some kind of loop or array but that is not a strong point of mine.
HTML
<form action="test.php" method="post" autocomplete="on" enctype="multipart/form-data" accept-charset='UTF-8'>
<fieldset><label for="Name">Name <sup>*</sup></label>
<input name="name" placeholder="full name" id="Name" required="" autofocus><br>
<label for="email">E-mail <sup>*</sup></label>
<input type="email" name="email" placeholder="youremail#domain.com" id="email" required=""><br>
<label for="phoneNumber">Phone # <sup>*</sup></label>
<input type="tel" name="phone" placeholder="XXX-XXX-XXXX" id="phoneNumber" required=""><br>
</fieldset>
<fieldset>
<label for="year">Year <sup>*</sup></label>
<input type="text" name="year" placeholder="year of car" id="year" required=""><br>
<label for="make">Make <sup>*</sup></label>
<input type="text" name="make" placeholder="make of car" id="make" required=""><br>
<label for="model">Model <sup>*</sup></label>
<input type="text" name="model" placeholder="model of car" id="model" required=""><br>
</fieldset>
<label for="State" class="state">State <sup>*</sup></label><br>
<input type="text" class="state" name="state" id="state" placeholder="Do not enter if you are human">
<fieldset>
<legend>Add Photos <sup>*</sup></legend>
<input type="file" name="attachment" required=""><br>
<input type="file" name="attachment"><br>
<input type="file" name="attachment"><br>
</fieldset>
<label for="comments">Comments</label><br>
<textarea style="float: left;" rows="4" name="comment" id="comments" placeholder="additional comments"></textarea>
<br>
<input class="button" type="submit" name="submit" value="Send" class="button">
<p class="required"><sup>*</sup> denotes a required field.</p>
</form>
PHP
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "
Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment
";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
else{
//process the rest of the form
}
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $name $email";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as
possible.";
}
else{
echo "Error in Email sending";
}
?>
All three of your fields are named "attachment". Give them unique names like "attachment1", "attachment2", etc.
Then use this on your upload:
for ($x = 1; $x <= $numberOfFiles; ++$x) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
}
You could even do it in a loop like this, which would allow you to add more file fields (maybe with JavaScript) without changing the backend code, as long as you kept the naming pattern consistent.
$x = 1;
while (isset($_FILES['attachment'.$x])) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
++$x;
}
That just increments x on each cycle, and checks to see if a file was sent with that name.
You should probably also include some checking on the size and type of files, before someone fills your inbox with huge files and/or cyber-nasties.
UPDATE: This should be a mostly-complete solution for you. I'm not able to test it for you at the moment so it may not work straight out of the box, but it should at least point you in the right direction. In this code snippet it sets up the initial part of the email first, then loops through and adds each file one at a time.
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
/* Start of headers */
$headers = "From: $name $email";
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Loop through files */
$x = 1;
while (isset($_FILES['attachment'.$x])) {
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Skip invalid files */
if (!file($tmpName)) {
++$x;
continue;
}
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
++$x;
}
/* Close the message */
$message .= "--{$mimeBoundary}--\n";
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as possible.";
} else {
echo "Error in Email sending";
}
?>

How do i add attach file to my online emailer PHP?

I hope you will help me with my question, because i really need it for my site.
How do i add the possibility to attach a file to a online emailer I've written in PHP? :)
Here is the code that i would like to add it to:
<?php
session_start();
if( isset($_POST['submit']))
{
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) )
{
$to=$_POST["to"];
$from=$_POST["from"];
$name=$_POST["name"];
$subject=$_POST["subject"];
$message=$_POST["message"];
$message=$message."\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
$head="From: ".$from."\r\n".'Reply-To: '.$From."\r\n";
$her=$head.' < '.$from.' >';
$techomag=mail($to, $subject, $message, $her);
echo "Your Message Has been send successfully...:)..";
unset($_SESSION['security_code']);
echo( 'Click here to send another email' );
}
else
{
echo 'Sorry, you have provided an invalid captcha security code :(...';
echo( 'Click here to Go back and try once more' );
}
}
else
{
?>
<?php
?>
<div style="height:10px; clear:both"></div>
<form action="index.php" method="post">
<table border="1"><tbody>
<tr><td>To Email :</td><td><input name="to" type="text" /></td></tr>
<tr><td>From Email :</td><td><input name="from" type="text" /></td></tr>
<tr><td>From Name :</td><td><input name="name" type="text" /></td></tr>
<tr><td>Subject :</td><td><input name="subject" type="text" /></td></tr>
<tr><td>Message :</td><td><textarea cols="30" rows="10" name="message"></textarea></td></tr>
<tr><td><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" />
<input type="submit" name="submit" value="Send Email!" /></td></tr>
</tbody></table>
</form>
<?php
}
?>
Well for complex emailing with PHP checkout the PHPMailer class.
It is much easier to send complex emails (e.g. with attachment) with the functions in PHPMailer.
<?php
$filename = "form.txt"; //attach filename
$to = "abc#mail.ru"; //To
$from = "def#gmail.com"; //From
$subject = "Test"; //Subj
$message = "Текстовое сообщение"; //Message
$boundary = "---"; //Delimitter
/* Headers */
$headers = "From: $from\nReply-To: $from\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$body = "--$boundary\n";
/* Text message */
$body .= "Content-type: text/html; charset='utf-8'\n";
$body .= "Content-Transfer-Encoding: quoted-printablenn";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= $message."\n";
$body .= "--$boundary\n";
$file = fopen($filename, "r"); //Открываем файл
$text = fread($file, filesize($filename)); //Считываем весь файл
fclose($file); //Закрываем файл
/* ADD attach */
$body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($filename)."?=\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
$body .= chunk_split(base64_encode($text))."\n";
$body .= "--".$boundary ."--\n";
mail($to, $subject, $body, $headers); //SEND
?>

Categories