Subscribe

RSS Feed (xml)

PHP-Email: Send Attachement Mail

After learn send email with dual format (HTML/text), we will learn how to send email with attachment. In this situation, we will make the primary MIME type be multipart/mixed so that we can do an attachment or two.

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

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>

PHP-Email: Sending a Simple HTML Mail (Part 2)

May be you ever get advertise email. They send you a html email. The layout is very nice. There are many tables, coloring font, and soon. Yeah, they use html mail. They just set the body of the email to have HTML in it and add one additional header of Content-type: text/html and have it work. Ok, look the code:

02// Setting a timezone, mail() uses this.
03date_default_timezone_set('America/New_York');
04// recipients
05$to  = "you@miscellaneous4all.com" . ", " ; // note the comma
06$to .= "we@miscellaneous4all.com";
07
08// subject
09$subject = "Test for HTML Format";
10
11// To send HTML mail, you can set the Content-type header.
12$headers  = "MIME-Version: 1.0\r\n";
13$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
14
15// additional headers
16$headers .= "To: You ,
17We miscellaneous4all.com>\r\n";
18$headers .= "From: Me miscellaneous4all.com>\r\n";
19$headers .= "Cc: he@miscellaneous4all.com\r\n";
20$headers .= "Bcc: she@miscellaneous4all.com\r\n";
21
22// Your message here:
23$body = "
24
25
26
27
28
29'red'>Hai
30
31
32";
33
34// Finally, send the email
35mail($to, $subject, $body, $headers);
36?>


Related Posts with Thumbnails