Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

MIME "content-type"

the mail() function

Moderators: macek, egami, gesf

MIME "content-type"

Postby hamwisk » Wed May 23, 2012 1:28 pm

Hello, I have a series of php scripts that all come together to make a nice email sending page. It all works, but i have one problem.

I would like to add a signature to the email, in html.

The signature is a variable ($end) so i don't know how i can send the correct version (html or plain text) as i will never know if the recipients mail client accepts html formatting.

Code: Select all
                     // open this directory
$Ulocation = ("/website/test/email/upload/");
$myDirectory = opendir($Ulocation);
                     // get each entry into an array
while($entryName = readdir($myDirectory)) {
   $files[] = $entryName;
}
$indexCount   = count($files);
                     // email fields: to, from, subject, and so on
switch ($_POST['from']){
   case "1":
      $from=($_POST[other]);
      break;
   case "2":
      $from="adam@website.co.uk";
      $end="\r\n\r\n Adam \r\n\r\n nwww.website.co.uk | adam@website.co.uk";
      break;   
   case "3":
      $from="eve@website.co.uk";
      $end="\r\n\r\n Eve \r\n\r\n www.website.co.uk | eve@website.co.uk";
      break;   
   case "4":
      $from="contact@website.co.uk";
      $end="\r\n\r\n www.website.co.uk | contact@website.co.uk";
      break;}
$to = $_POST['to'];
$cc = $_POST['cc'];
$bcc = $_POST['bcc'];
$subject = $_POST['subject'];
$message = stripslashes($_POST['message'].$end);
$headers = "From: $from" . "\r\n" .
$headers = "Cc: $cc" . "\r\n" .
$headers = "Bcc: $bcc";
                     // boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                     // headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                      // multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
                     // preparing attachments
for($index=2; $index < $indexCount; $index++) {
   $file = fopen($Ulocation.$files[$index],"rb");
   $data = fread($file,filesize($Ulocation.$files[$index]));
   fclose($file);
   $data = chunk_split(base64_encode($data));
   $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$index]\"\n" .
   "Content-Disposition: attachment;\n" . " filename=\"$files[$index]\"\n" .
   "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
   $message .= "--{$mime_boundary}\n";
}
closedir($myDirectory);
$ok = @mail($to, $subject, $message, $headers);
if ($ok){...


Here is what i'm thinking...

If i change the "content-type" to "multipart/alternative" then i get so confused.
If i made two signatures, $end and $End with one having the html formatting, then i can do it i think... i will try to do this myself (but that doesnt mean i don't want help, please god someone help me) and update any changes i come up with.
Last edited by hamwisk on Wed May 23, 2012 5:29 pm, edited 1 time in total.
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am

Re: MIME "content-type"

Postby hamwisk » Wed May 23, 2012 4:10 pm

Ah ha, i'm seeing it now...
Code: Select all
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . $end . "\n\n";
$message .= "--{$mime_boundary}\n";

I've moved the $end to here now, small changes make big differences, if i can add another bit in that looks the same but with a $HTMLend instead of the $end and with the content type different of course.
Something like:
Code: Select all
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . $HTMLend . "\n\n";
$message .= "--{$mime_boundary}\n";


but i need to change thie header:
Code: Select all
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

to:
Code: Select all
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/alternative;\n" . " boundary=\"{$mime_boundary}\"";

But it isnt working as i expected when i make the changes. What am i doing wrong?
:offtopic: I never capitalise my i's except when it's the first word in a sentence, sorry.
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am

Re: MIME "content-type"

Postby hamwisk » Wed May 23, 2012 5:25 pm

Hello again, i've changed some stuff around...
i've added an extra variable called $HTMLend which has a signature with html formatting
Code: Select all
   case "4":
      $from="contact@website.co.uk";
      $end="\r\n\r\n Contact \r\n\r\n www.website.co.uk | contact@website.co.uk";
      $HTMLend="<h1> Contact </h1><br /><p>www.website.co.uk | contact@website.co.uk</p>";
      break;


I changed the initail $message into $Message, this is because when it was just $message the text/html bit would use the $message from the text/plain $message which would include the $end, this way the works.
Code: Select all
$Message = stripslashes($_POST['message']);


I've changed the header for the MIME type to multipart/alternative
Code: Select all
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/alternative;\n" . " boundary=\"{$mime_boundary}\"";


I've added the alternative content so that bit and the plain text bit look like:
Code: Select all
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $Message . $end . "\n\n";
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: quoted-printable\n\n" . $Message . $HTMLend . "\n\n";
$message .= "--{$mime_boundary}\n";


But alas my best efforts and it still doesnt work. I just get a blank email, no header even... The original message sent is thus:
Code: Select all
To: xxxxxxx@gmail.com
Subject: Email testing
From: contact@website.co.uk
Cc:
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="==Multipart_Boundary_x5eb7aafe97e072b37d8e5d8ff653eb2ex"
Message-Id: <E1SXMbU-0000oQ-8z@xxxxxxxxxxx.net>
Date: Thu, 24 May 2012 02:12:36 +0100
X-Original-User: userxxxxx

This is a multi-part message in MIME format.

--==Multipart_Boundary_x5eb7aafe97e072b37d8e5d8ff653eb2ex
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

It would be really nice if you'd just work, so i can go to bed...

 Contact

 www.website.co.uk | contact@website.co.uk

--==Multipart_Boundary_x5eb7aafe97e072b37d8e5d8ff653eb2ex
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

It would be really nice if you'd just work, so i can go to bed...<h1> Contact </h1><br /><p>www.website.co.uk | contact@website.co.uk</p>

--==Multipart_Boundary_x5eb7aafe97e072b37d8e5d8ff653eb2ex
Content-Type: {"application/octet-stream"};
 name="1335475221018.jpg"
Content-Disposition: attachment;
 filename="1335475221018.jpg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEP
ERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAMABAADAREA
.......loads more like this but you don't want to see that i'm sure.......
--==Multipart_Boundary_x5eb7aafe97e072b37d8e5d8ff653eb2ex


Whats clear here is that i have little understanding of the alternative content type. Why does gmail not display anything except the attachment? I might sleep on this one, i'm going to be dreaming of semi-colons and slashes again :yawn:
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am

Re: MIME "content-type: not/working"

Postby hamwisk » Wed May 23, 2012 7:06 pm

Yes! I've nearly solved this one... but still i'm so far :(
Now gmail displays the message, but it's showing both messages one after the other. Here's what i've done:
Code: Select all
$headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: multipart/alternative;\n\n";
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $Message . $end . "\n\n";
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n" . "Content-Transfer-Encoding: quoted-printable\n\n" . $Message . $HTMLend . "\n\n";
$message .= "--{$mime_boundary}\n";

I realised the header must have the "multipart/mixed" bit, so i added the multipart/alternative in the message. Although the message is now being displayed (an improvement) my mail client (gmail) is not picking one but using both, so the message is duplicated.
I also had to change the charset for the html type to utf-8 as the signature has an accent (é).
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am

Re: MIME "content-type"

Postby hamwisk » Wed May 23, 2012 9:34 pm

Ok i'm really proud of myself. Nearly sorted everything (still want :help: though) but theres one niggling problem.
The alternative content type is great, but when i include an attachment, my email client shows no text. I read loads of pages on the MIME standard and i learnt this -- content-type: multipart/alternative means that the email program will pick the best content type of the given options, and the most basic comes first yadi yada yada.
So that means i need to send multipart/mixed emails, but thats no good either.

By the way, i think the problem i was having with the alternative content type was simply the line breaks. because i re-wrote it all, making it neater, and then pow it worked. here is the updated code:
Code: Select all
$Message = stripslashes($_POST['message']);
$headers = "From: $from\r\n";
$headers .= "Cc: $cc\r\n";
$headers .= "Bcc: $bcc\r\n";
                     // make a boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                     // additional headers
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"\r\n";
$message = "This is a multi-part message in mime format.\r\n\r\n";
                     // plain text
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . $Message . $end . "\r\n\r\n";
                     // html format
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\r\n" . "Content-Transfer-Encoding: quoted-printable\r\n\r\n" . $Message . $HTMLend . "\r\n\r\n";
                     // preparing attachments
for($index=2; $index < $indexCount; $index++) {
   $file = fopen($Ulocation.$files[$index],"rb");
   $data = fread($file,filesize($Ulocation.$files[$index]));
   fclose($file);
   $data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: {\"application/octet-stream\"};\r\n" . " name=\"$files[$index]\"\r\n" .
"Content-Disposition: attachment;\r\n" . " filename=\"$files[$index]\"\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n" . $data . "\r\n\r\n";
$message .= "--{$mime_boundary}\r\n";
}


Oh i think i'm making progress then i realise that ive got more, and more to learn, i won't sleep until i understand this though.

ps is what i'm trying to do actually hard or am i just being stoopid?
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am

solved

Postby hamwisk » Wed May 23, 2012 11:01 pm

I needed to have two different boundries. So it works like this, the message is multipart/mixed, and within that is:
1, an alternative content type, with a different boundry
a. the plain text version
b. the html version
2, the attachment

I am so chuffed, here's the entire code:
Code: Select all
<?php
                     // open this directory
$Ulocation = ("/website/test/email/upload/");
$myDirectory = opendir($Ulocation);
                     // get each entry into an array
while($entryName = readdir($myDirectory)) {
   $files[] = $entryName;
}
$indexCount   = count($files);
                     // email fields: to, from, subject, and so on
switch ($_POST['from']){
   case "1":
      $from=($_POST[other]);
      break;
   case "2":
      $from="adam@website.co.uk";
      $end="\r\n\r\nAdam \r\n\r\nwww.website.co.uk | adam@website.co.uk";
      $HTMLend="\r\n\r\n<h1> Adam </h1><br /><p>www.website.co.uk | adam@website.co.uk</p>";
      break;   
   case "3":
      $from="eve@website.co.uk";
      $end="\r\n\r\Eve \r\n\r\nwww.website.co.uk | eve@website.co.uk";
      $HTMLend="\r\n\r\n<h1> Eve </h1><br /><p>www.website.co.uk | eve@website.co.uk</p>";
      break;   
   case "4":
      $from="contact@website.co.uk";
      $end="\r\n\r\nContact \r\n\r\nwww.website.co.uk | contact@website.co.uk";
      $HTMLend="\r\n\r\n<h1> Contact </h1><br /><p>www.website.co.uk | contact@website.co.uk</p>";
      break;
}
$to = $_POST['to'];
$cc = $_POST['cc'];
$bcc = $_POST['bcc'];
$subject = $_POST['subject'];
$Message = stripslashes($_POST['message']);
$headers = "From: $from\r\n";
$headers .= "Cc: $cc\r\n";
$headers .= "Bcc: $bcc\r\n";
                     // make a boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                     // additional headers
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n";
$message = "This is a multi-part message in mime format.\r\n\r\n";
                     // alternative types
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: multipart/alternative; boundary=\"{$mime_boundary}z\"\r\n";
                     // plain text
$message .= "--{$mime_boundary}z\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . $Message . $end . "\r\n\r\n";
                     // html format
$message .= "--{$mime_boundary}z\r\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\r\n" . "Content-Transfer-Encoding: quoted-printable\r\n\r\n" . $Message . $HTMLend . "\r\n\r\n";
                     // preparing attachments
for($index=2; $index < $indexCount; $index++) {
   $file = fopen($Ulocation.$files[$index],"rb");
   $data = fread($file,filesize($Ulocation.$files[$index]));
   fclose($file);
   $data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: {\"application/octet-stream\"};\r\n" . " name=\"$files[$index]\"\r\n" .
"Content-Disposition: attachment;\r\n" . " filename=\"$files[$index]\"\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n" . $data . "\r\n\r\n";
$message .= "--{$mime_boundary}\r\n";
}
closedir($myDirectory);
                     // check cc bcc and send
$ok = @mail($to, $subject, $message, $headers);
if ($ok){
if ($cc . $bcc == ""."") {
   echo "<center><h2>Congratulations!</h2><br ><p>Your message has been sent to $to!</p>";
   include ("success.php");
   } else
if ($bcc == "") {
   echo "<center><h2>Congratulations!</h2><br ><p>Your messages have been sent to $to and $cc!</h2>";
   include ("success.php");
   } else
if ($cc == "") {
   echo "<center><h2>Congratulations!</h2><br ><p>Your messages have been sent to $to and $bcc!</p>";
   include ("success.php");
   } else
echo "<center><h2>Congratulations!</h2><br ><p>Your messages have been sent to $to, $cc and $bcc!</p>";
include ("success.php");
} else
echo "<h1>Error sending message!</h1><br /><p>Use your browsers back button to avoid loosing previous form data.</p>";
?>


And the email it produces:
Code: Select all
To: me@gmail.com
Subject: Email testing 3
From: eve@website.co.uk
Cc:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0x"
Message-Id: <don't know if i should show this>
Date: Thu, 24 May 2012 07:45:59 +0100
X-Original-User: or this

This is a multi-part message in mime format.

--==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0x
Content-Type: multipart/alternative; boundary="==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0xz"
--==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0xz
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

please??

Eve

www.website.co.uk | eve@website.co.uk

--==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0xz
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

please??

<h1> Eve </h1><br /><p>www.website.co.uk | eve@website.co.uk</p>

--==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0x
Content-Type: {"application/octet-stream"};
 name="cat.jpg"
Content-Disposition: attachment;
 filename="cat.jpg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAgEAYABgAAD/7gAOQWRvYmUAZAAAAAAB/+EQskV4aWYAAE1NACoAAAAIAAKH
it goes on and on so i skipped a bit for you
3v3Xuve/de697917r//Z


--==Multipart_Boundary_xf9db539375c97e1b81f6994b4dbc21c0x
hamwisk
New php-forum User
New php-forum User
 
Posts: 11
Joined: Fri Mar 02, 2012 4:59 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.