I'm using the following code to send an email with attachments:
$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$message_body\r\n";
$message .= "--".$mime_boundary."\r\n";
foreach($attachments as $filename => $data)
{
$message .= "Content-Type: application/octet-stream;\r\n";
$message .= " name=\"$filename\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"$filename\"\r\n";
$message .= "\r\n";
$message .= chunk_split(base64_encode($data));
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
}
mail($email_address, $email_subject, $message, $headers);
Which works fine, except that an extra file is also attached (called "Part 1.4").
Is there a way to not have this added?
Cheers,
Dan.
IIRC the last part separator must be --something unique--, i.e. in your case
$message .= "--".$mime_boundary."--\r\n";
But mime mail is more or less a solved problem ( i.e. for an application developer it's boring when done correctly and reeeeally annoying when done wrong ;-) ). Do yourself a favor and use something like Swiftmailer or any other descend mailing library/class.
Related
I'm trying to use the built-in PHP mail function to send multipart messages that contain a html and a plain text version of my message. I've been playing around with different encoding types but, I keep running into problems. Originally I set Content-Transfer-Encoding to Binary but, that resulted in exclamation points being placed every 78 characters. I also tried base64 but I believe that base64 is overkill for what I am doing.
All I'm doing is sending basic HTML, no encoded images, files, or attachments. I'd prefer an encoding method that would still allow the source code to be human readable.
I heard that Quoted-Printable is what I'm looking for but, when I attempted to send messages using that encoding type the result ending up looking really weird. I noticed a bunch of " symbols sprinkled throughout the message source code.
Here is the code I'm using:
$to = "to#test.com";
$subject = "test subject";
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\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";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
What the heck am I doing wrong here?
Hi try the following code,
$to = "to#test.com";
$subject = "test subject";
$plainTextMessage = "Hi all";
$HTMLmessage = "<b>Hi all</b>";
//$boundary = uniqid('np');
$boundary = md5(uniqid(time()));
$headers .= "From: from-address#test.com\r\n";
$headers .= "Reply-To: reply-address#test.com\r\n";
$headers .= "Return-Path: return-path#test.com\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";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
May it will help
No need for a third party library or an external mail forwarding host. This is what I use and it works like a charm. It also fixes some potential security holes by forcing headers using the $from address that can otherwise reveal your system user :
private function sendMail($to, $from, $fromName, $subject, $text, $html)
{
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: ".$fromName." <".$from.">" . "\r\n";
$headers .= "X-Sender: ".$fromName." <".$from.">\n";
$headers .= "Return-Path: <".$from.">\n";
$headers .= "To: ".$to."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Content body
$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";
// Plain text body
$message .= $text;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// Html body
$message .= $html;
$message .= "\r\n\r\n--" . $boundary . "--";
// Send mail
mail('', $subject, $message, $headers, '-f ' . $from);
}
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";
I'm struggling with sending my multipart email with plain text version as well as with HTML. Unfortunately when I receive it in Gmail it shows as downloadable message only as shown below:
The code is pasted below and although I went over like 100 questions here on SO I'm still stuck.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: donotreply#someaddress.com\r\n";
$headers .= "Subject: Test mail\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";
// plain text version
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// html version here
$message .= $htmlMessage;
$message .= "\r\n\r\n--" . $boundary . "--";
return #mail($to, $subject, $message, $headers);
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
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