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
Related
I am using php mail function with PDF attachment. Here everything is working fine but in the email body this following warning comes
"This email has an attachment that allows unverified scripts to run on your computer when opened. Be careful"
can you suggest ? what is the problem
Are you using the correct headers for sending attachments?
You can try using this pattern (tested and working):
$subject = 'SUBJECT';
$message .= 'Hello world'."\n";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: ME <me#mymail.org>".$eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol;
$body = "--".$separator.$eol;
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol;
$body .= $message.$eol;
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol;
$body .= $content.$eol;
$body .= "--".$separator."--";
$filename is your attachment, $file is the full path (including $filename).
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've checked php manual, but still can't get my script to work
If I delete the BCC line, the mail goes to $to1 but with BCC line, it goes nowhere
here is the code:
// email stuff
$to1 = "address1#mail.com";
$to2 = "address2#mail.com";
$from = "noreply#mail.com";
$subject = "Subject";
$message = "message";
// 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
$headers = "To: ".$to1.$eol;
$headers .= "From: ".$from.$eol;
$headers .= "Bcc: ".$to2.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol;
$body .= $message.$eol;
// send message
mail($to1, $subject, $body, $headers);
If anyone has an idea, thanks a lot
I run PHP on IIS6. I have some PHP that successfully sends a 1KB image as an attachment on an email. When I try and attach a 500KB PDF however (having changed the Content-Type), it hangs and after a few minutes I get "FastCGI process exceeded configured request timeout" (Error Number 258 (0x80070102)).
Any thoughts on why it's taking so long to attach the PDF? The solution is not to increase the timeout limit, I can't have users sitting there for 3+ minutes while the file gets sent.
I've included my code below:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .="This is a multipart message in MIME format. \r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$headers .= $text . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $html . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
$headers .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$headers .= $attachment . "\r\n\r\n";
$headers .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $text, $headers );
Thanks in advance for any advice.
Put the attachment in the message parameter of the mail() function instead of the additional headers parameter.
I ran into the same problem today and found that I couldn't submit large files as part of the headers parameter in the mail() function.
e.g.
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$body .="This is a multipart message in MIME format. \r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$body .= $text . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename)));
$body .= $attachment . "\r\n\r\n";
$body .= "--".$uid."\r\n\r\n";
//send the email
$mail_sent = #mail( $to, $subject, $body, $headers );
I've created a form which contains an upload field file and some other text fields. I'm using php to send the form's data via email and attach the file.
This is the code I'm using but it's not working properly. The file is normally attached to the message but the rest of the data is not sent.
$body="bla bla bla";
$attachment = $_FILES['cv']['tmp_name'];
$attachment_name = $_FILES['cv']['name'];
if (is_uploaded_file($attachment)) {
$fp = fopen($attachment, "rb");
$data = fread($fp, filesize($attachment));
$data = chunk_split(base64_encode($data));
fclose($fp);
}
$headers = "From: $email<$email>\n";
$headers .= "Reply-To: <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $first_name $family_name<$email>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <$email>\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= "$body\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
$subject = 'bla bla bla';
$to="test#test.com";
mail($to,$subject,$message,$headers);
Why isn't the $body data not sent? Can you help me fix it?
Well, I'd suggest using the PEAR Mail_Mime package... It abstracts all that away...
As for your exact issue, I'd guess it's because you have two different boundaries and two Content-Type headers in the header section. Try generating something like this:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------=MIME_BOUNDARY_MESSAGE_PARTS"
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: text/html charset="utf-8"
$body
------=MIME_BOUNDARY_MESSAGE_PARTS
Content-Type: application/octet-stream;name="filename"
Content-Transfer-Encoding: base64
...
$data
------=MIME_BOUNDARY_MESSAGE_PARTS