Subscribe

RSS Feed (xml)

PHP-Email: Sending Dual Format (Part 3)

When we send only html email, it still have problems. How if someone is using a mail client that cannot understand HTML email. To solve this problems, it is best to send any HTML email as dual format. This means providing both a text and an HTML version in the same email. Your client can choose which version to display.



01<?php
02// Setting a timezone, mail() uses this.
03date_default_timezone_set('America/New_York');
04// recipients
05$to  = "you@phpeveryday.com" . ", " ; // note the comma
06$to .= "we@phpeveryday.com";
07 
08// subject
09$subject = "Test for HTML Format";
10 
11// Create a boundary string.  It needs to be unique
12$sep = sha1(date('r', time()));
13 
14// Add in our content boundary, and mime type
15//specification:
16$headers .=
17    "\r\nContent-Type: multipart/alternative;
18boundary=\"PHP-alt-{$sep}\"";
19 
20// additional headers
21$headers .= "To: You <you@phpeveryday.com>,
22We <we@phpeveryday.com>\r\n";
23$headers .= "From: Me <me@miscellaneous4all.com>\r\n";
24$headers .= "Cc: he@miscellaneous4all.com\r\n";
25$headers .= "Bcc: she@miscellaneous4all.com\r\n";
26 
27// Your message here:
28$body =<<<EOBODY
29--PHP-alt-{$sep}
30Content-Type: text/plain
31 
32Hai, It's me!
33 
34--PHP-alt-{$sep}
35Content-Type: text/html
36 
37<html>
38<head>
39<title>Test HTML Mail</title>
40</head>
41<body>
42<font color='red'>Hai, it is me!</font>
43</body>
44</html>
45 
46--PHP-alt-{$sep}--
47EOBODY;
48 
49// Finally, send the email
50mail($to, $subject, $body, $headers);
51?>
52</me@miscellaneous4all.com>

0 comments:

Related Posts with Thumbnails