Utkarsh Dixit
Utkarsh Dixit

Reputation: 4275

What are -- in email headers?

Recently i came across -- as a header of mail() function in php. I tried to google it out but didn't found any result so i am posting this question. While trying to edit my code i found that the id after these two -- should be unique.My php script to send email with attachement.

<?php

$file = "http://wpbrisk.com/download/email/email/doc1.pdf"; // Specify the Url of the attachment here

$filename = "doc1.pdf"; // Specify the name of the file here

$to="[email protected]"; // Enter the mail to which you want to sent the attachement
$your_name = "Hudixt"; // Your name
$from_mail = "[email protected]"; // Your email

$subject = "This is a mail with attachment."; // Subject
$message = "<div id='hello'>World</div>"; // message

// Get the content of the file
    $content = file_get_contents($file);

    $content = base64_encode($content);
    $uid = md5(uniqid(time()));

    $header = "From: ".$from_name." <".$from_mail.">\r\n";

    $header .= "MIME-Version: 1.0\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";
    $header .= "--".$uid."\r\n";


    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";

    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here

    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    mail($mailto, $subject, "", $header);
        ?>

Can you please tell me what this -- means in header and why it should be unique.

Upvotes: 0

Views: 1189

Answers (2)

AdrianH
AdrianH

Reputation: 76

Seems like a MIME boundary to me...

Basically, when you have multiple types of content in an e-mail message, the different parts are separated using an ID. Each part is then preceded by '--' followed by the boundary. From the RFC:

Thus, a typical multipart Content-Type header field might look like this:

 Content-Type: multipart/mixed; 
      boundary=gc0p4Jq0M2Yt08jU534c0p

This indicates that the entity consists of several parts, each itself with a structure that is syntactically identical to an RFC 822 message, except that the header area might be completely empty, and that the parts are each preceded by the line --gc0p4Jq0M2Yt08jU534c0p

See http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html for more info.

To answer your last question, when you have an e-mail with text and attachment, the whole message will have Content-Type: multipart/mixed, the text will be text/plain, the attachment (suppose a word file) will be application/msword. Each new content-type part will be separated by '--' . $boundary. The boundary is chosen to be unique and different from the other message parts so that it doesn't appear inside either of them - when the application interpreting the e-mail contents finds $boundary it must be safe to assume that's an intended boundary and not just a fluke inside the message.

Hope that helps!

Upvotes: 1

MrTux
MrTux

Reputation: 34002

These "--ID--" lines are MIME part boundaries (see https://en.wikipedia.org/wiki/MIME). They are described in https://www.rfc-editor.org/rfc/rfc2046. They are used to separate different parts of a message.

According to RFC#2046 those only must be unique "enough". If you want to encapsulate an email into another one, the boundaries must not infer with each other, i.e. they have to be distinct. So it's a good idea to make them as unique "as possible and needed":

This implies that it is crucial that the composing agent be able to choose and specify a unique boundary parameter value that does not contain the boundary parameter value of an enclosing multipart as a prefix. (https://www.rfc-editor.org/rfc/rfc2046#section-5.1)

Upvotes: 1

Related Questions