I have the below code which is executed when someone hits a send button on a form.
My issue is I am receiving an email with the correct subject line and from/to email address. However if the email contains an attachment the body then is blank. However if their is no email attachment the email body gets populated with the text the user would of typed in.
I should note I understand that the code is not as safe as in its current state does not limit inappropriate files like exe's from being uploaded. However I am not wanting or needing a lesson in how to make the code safe. I just need help with how/why the text body does not get populated when an attachment is uploaded?
<?php
//send email
if(isset($_POST['sendemail']))
{
$txtSid = md5(uniqid(time()));
$emailsubject = $_POST['emailsubject'];
$emailpriority = $_POST['emailpriority'];
$emailto = $_POST['emailto'];
$emailcc = $_POST['cc'];
$emailbcc = $_POST['bcc'];
$txtFilesName = $_FILES["attachment"]["name"];
$emailbody = $_POST['wysiwg_editor'];
//if their is an attachment add notice in body of email
if($txtFilesName !='')
{
$emailbody = "$emailbody <br /><br />File name $txtFilesName attached to this email.";
}
$headers = "From: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "Reply-To: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "CC: $emailcc\r\n";
$headers .= "BCC: $emailbcc\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= $emailbody."\n\n";
if($emailpriority == 'high priority')
{
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
}
$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
$txtFilesName = $_FILES["attachment"]["name"];
$txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
$headers .= "--".$txtSid."\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
$headers .= $txtContent."\n\n";
}
mail("$emailto","$emailsubject","$emailbody","$headers");
echo "<div class='alert alert-success'>Email Sent!</div>";
}
OK I re-copy and pasted the code I am using and it has fixed my issue. It seems that I needed a $headers .= $emailbody."\n\n"; line of code inside the if email attachment section.
Under the if statement that detects if there is an attachment uploaded, you are not combining the previous emailbody and the notice together. Instead, use this code:
$emailbody = $emailbody."<br /><br />File name ".$txtFilesName." attached to this email.";
Related
What's wrong in my code?
Mail sending fine and text also fine but the attachment showing like noname.txt
Please help.
$upload_name=$_FILES["resume"]["name"];
$upload_type=$_FILES["resume"]["type"];
$upload_size=$_FILES["resume"]["size"];
$upload_temp=$_FILES["resume"]["tmp_name"];
$file = $upload_temp;
$content = chunk_split(base64_encode(file_get_contents($file)));
$num = md5(uniqid(time()));
$email_to = "noname#example.com";
$email_subject = $name . " Applied for a job in website careers page";
$email_message = "<b>Form details below.</b><br />";
$email_message .= "<tr><td><strong>Last Name</strong> </td><td>".$name."</td></tr>";
$email_message .= "<tr><td><strong>Phone</strong> </td><td>".$phone."</td></tr>";
$headers .='Reply-To: '. $email_to . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$num."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$num."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$headers .= $email_message."\r\n\r\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type."; name=\"".$upload_name."\"\r\n\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$upload_name."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$num."--";
if(#mail($email_to, $email_subject, "", $headers)){
echo "Mail Send";
}
else {
echo "Mail not sent.";
}
Please help. Thanks in advance.
Have you consider using PHPMailer? In my experience, saves yourself from a lot of these hassles.
Otherwise, try changing:
$headers .= "Content-Type:".$upload_type.";name=\"".$upload_name."\"\r\n\r\n";
To:
$headers .= "Content-Type: multipart/mixed; name=\"".$upload_name."\"\r\n\r\n";
Am trying to create a mail form with attachment where the reply address is different from the received address. They receiving emails works properly but if you decide to reply to them, it adds mime-version to the end of the reply email. (ex: email#gmail.commime-version)
See code below.
<?php
// Email address to which you want to send email
$to = $_POST["gut"];
$subject = $_POST["fieldSubject"];
$message = nl2br($_POST["fieldDescription"]);
/*********Creating Uniqid Session*******/
$txtSid = md5(uniqid(time()));
$headers = "";
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."";
$headers .= "MIME-Version: 1.0" . "\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";
/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
$txtFilesName = $_FILES["attachment"]["name"];
$txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
$headers .= "--".$txtSid."\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
$headers .= $txtContent."\n\n";
}
// # is for skiping Errors //
$flgSend = #mail($to,$subject,null,$headers);
if($flgSend)
{
echo 'Your email as being sent successFully.';
}
else
{
?>
What happens if you change:
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."";
To
$headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."\n";
Notice the \n at the end of the line.
I was testing a PHP Form for my client, when I realized that file attachment wasn´t optional, I have the full code here (it´s pretty long). Form works fine, but if you don´t choose any file, the e-mail will never arrive to it´s destination. I want to send the mail anyway and receive it without the attach.
I´m using PHP and jForm for validation.
Here is the input tag for file attaching (there to many input tags at the form.php file to list them here)
<input type="file" name="adjunto" id="adjunto">
And here is the send.php file (the true form is too long to publish here, i´m writing a shorter version of it)
<?php
// THE USER CHOOSES FROM A JOB LIST
$busqueda = $_POST["busqueda-list"];
$cargo = $_POST["cargo-list"];
// FILLS PERSONAL INFORMATION
$nombre = $_POST["nombre"];
$apellido = $_POST["apellido"];
$sexo = $_POST["sexo-list"];
// WE STYLE A MESSAGE WITH LOTS OF INPUTS
$mensaje = 'In the real form here is a table with lots of input styled';
// AND THEN WE ATTACH (in spanish spells adjunto)
$adjunto_name=$_FILES["adjunto"]["name"];
$adjunto_type=$_FILES["adjunto"]["type"];
$adjunto_size=$_FILES["adjunto"]["size"];
$adjunto_temp=$_FILES["adjunto"]["tmp_name"];
// IF THERE IS A FILE ATTACHED
if (is_uploaded_file($adjunto_temp))
{if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
// IF NOT, JUST SEND MAIL
else
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
?>
Please I need a hand with this, I´m doing my best (i´ve already search here and other forums) and I think this maybe a problem many people had before!
Thank you!
you need to give one more condition on top
if (is_uploaded_file($adjunto_temp)) {
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
........... // your other stuff with respect to headers
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
}
}
else
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje);
now the mail will go on both conditions.
UPDATED WITH YOUR CODE
if (is_uploaded_file($adjunto_temp))
{
if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
{
// MEET THE HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// MAIL HEADERS with attachment
$fp = fopen($adjunto_temp, "rb");
$file = fread($fp, $adjunto_size);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Normal headers
$headers = "From: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With mensaje
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$mensaje."\n";
$headers .= "--".$num."\n";
// Attachment headers
$headers .= "Content-Type:".$adjunto_type." ";
$headers .= "name=\"".$adjunto_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$adjunto_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
// WE SEND MAIL WITH HEADERS
#mail("name#example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);
fclose($fp);
}
}
// IF NOT, JUST SEND MAIL
else
{
// MAIL HEADERS
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$email."\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
#mail("ezemosho#gmail.com", "".$busqueda." ".$cargo."", $mensaje);
}
Put the else statement for your first if condition, in your code you are using else for the condition2.
if (condition1) {
if (condition2) {
.........
.........
}
} else {
}
I have an order form that is processed and the customer receives the email, however, adding in the admin bit doesn't email the admin?
The following code is the bit that sends to the customer (which works fine) and also the code that should also email the admin but doesn't :(
// email stuff (change data below)
$to = $payment_data['sold_to_email'];
$from = $order_options['admin_email'];
$subject = "Your Order";
//include the message
$message = $order_options['email_message'];
//Build the Order out
$message .= '<br /><br />Thank you for purchasing from us';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: ". $company . " <" . $from . ">". $eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
//$headers .= $message.$eol.$eol;
// send message
$sendit = wp_mail($to, $subject, $message, $headers);
$admin_message = $payment_data['admin_field'];
wp_mail($admin_email, 'New Order', $admin_message);
//Code to troubleshoot email problems.
/* if(!$sendit) {
$tempOptions = get_option('order_options');
$tempOptions['admin_email'] = "EMAIL FAILED";
update_option('order_options', $tempOptions);
}
*/
We are trying to send an email using sendmail. Everything works fine with normall headers but the moment we add attachment in the header, the sender name comes as Apache. Here is our code snippet
$from_email = "noreply#domain.com";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "attachment.pdf";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$text = "Hi!";
// main header (multipart mandatory)
$headers = "From:".$from_email.$eol;
$headers = "Bcc:user#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $text.$eol.$eol;
// attachment
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--".$eol;
$b = mail($email, "Your Issue of the STQ",$message, $headers, "-fnoreply#domain.com");
By Adding -fnoreply#domain.com, we are getting like this in email header From: noreply#domain.com (Apache). Not sure where this Apache is coming from?
What could be the problem here.
Thanks
You need a dot on the second line.
$headers = "From:".$from_email.$eol;
$headers .= "Bcc:user#domain.com".$eol;
Make the header like this :
$headers .= 'From: <webmaster#example.com>' . "\r\n";
Also missing the dot on the second line as xyzz pointed