[advanced search]
Results from the most recent live auction are here.
20 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming > CODE
User Name
Password

Old 01-21-2007, 10:08 AM   · #1
beaver6813
NamePros Regular
 
beaver6813's Avatar
 
Name: Sam Cleaver
Location: England
Trader Rating: (11)
Join Date: May 2005
Posts: 327
NP$: 82.00 (Donate)
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
Mail In Both Plain Text And HTML At The Same Time!

Hey,

I was looking around for ages trying to get this damn thing working with thunderbird. Basically it sends an email using multipart/alternative which, if the email client supports MIME lets it decide whether to show the email in plain text or HTML.

PHP Code:
<?php
/*
Sam Cleaver aka Beaver6813 (http://www.beaver6813.com)
Mutlipart Email Script, Sends In Both Plain And HTML For Compatibility
Which Works With MIME 1.0+ Supporting Software.
Email: samcleaver.beaver[-at]gmail[-dot]com
Copyright (C) 2007  Beaver6813

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
Created By Beaver6813, a special thanks to Jonathan at The PHP Solution
for all your help along the way.
CHANGES:
-- Is Now Compatible With GMail
-- Encrypts Messages In Base64 For Easier Transportation
NOTES:
-- From experience with different mail services i've discovered some key pointers
when sending html or any type of email:
  * DO NOT link to external CSS stylesheets, most of the time these are either filtered out
  or do not load properly
  * As discovered in GMail it strips out any stylesheets completely, even inline ones so you're
  going to have to use the style='' tags on any div's etc you want to make look pretty
  * Bear in mind linking to any external resources will bring up a warning on the clients
  end and they can choose to or not to load external resources.
  * Keep HTML resources as simple as possible, most of the time they aren't loaded as well as
  they would in firefox and so can be really mucked up if you do some complicated magic with
  them.
COMING SOON:
-- Will be turned into a class making it easier to send emails :)
*/

$_emailer_subject = "My First Email Yay!"; //The subject of the email... duh
$_emailer_to = "samcleaver.beaver@gmail.com"; //You can make this even easier for the
//email client by specifying the actual name of the person as well, but for
//simplicities sake we'll just give the email
$_emailer_from = "test@localhost"; //Again, you can do to this what
//i said above
$_emailer_reply_to = "test@localhost";
$_emailer_cc = ''; //Optional
$_emailer_bcc = '';//Optional

//Okay what do you want to appear if it displays the plain text version?
$email_html = "<h1>HTML E-mail</h1>
<p>This is an <b>HTML</b> e-mail.</p>"
;
//Okay what do you want to appear if it displays the HTML version?
$email_plain = "HTML E-mail\n\nThis is the text portion of an HTML e-mail\n";

$_emailer_subject = $_emailer_subject == '' ? 'No Subject' : $_emailer_subject; // Must have a subject

$_emailer_cc = substr( $_emailer_cc , 0 , -2 ); // Remove the trailing ", "
$_emailer_bcc = substr( $_emailer_bcc , 0 , -2 );
$_emailer_reply_to = $_emailer_reply_to == '' ? $_emailer_from : $_emailer_reply_to;
    
$mid = md5( uniqid( time() ) ); // Generate a unique id for the email
$host = str_replace( 'www' , '' , $_SERVER['HTTP_HOST'] ); // Remove the www for an "email address"
    
$_emailer_cc_do = $_emailer_cc != '' ? "Cc: $_emailer_cc\n" : '';
$_emailer_bcc_do = $_emailer_bcc != '' ? "Bcc: $_emailer_bcc\n" : '';
    
# Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
  
$eol="\r\n";
} elseif (
strtoupper(substr(PHP_OS,0,3)=='MAC')) {
  
$eol="\r";
} else {
  
$eol="\n";
}

//add From: header
$headers = "From: $_emailer_from$eol";

//specify MIME version 1.0
$headers .= "MIME-Version: 1.0$eol";

$headers .= "Return-Path: $_emailer_reply_to$eol";

$headers .= "$_emailer_cc_do$eol";
$headers .= "$_emailer_bcc_do$eol";

$headers .= "Message-ID: <$mid@$host>$eol";

$headers .= "X-Priority: 3$eol";
$headers .= "X-MSMail-Priority: Normal$eol";
$headers .= "X-Mailer: PHP$eol";
$headers .= "X-MimeOLE: Generated By Beaver6813s MultiPart Mail Script$eol";

$headers .= "Date: " . gmdate('D, d M Y H:i:s Z' , time() ) . $eol;

//unique boundary this can be whatever you want... but try and make it imaginative :)
$boundary = uniqid("BEAVER6813");

//tell e-mail client this e-mail contains//alternate versions
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n";

//plain text version of message
$body = "--$boundary$eol" .
   
"Content-Type: text/plain; charset=ISO-8859-1$eol" .
   
"Content-Transfer-Encoding: base64$eol$eol";
$body .= chunk_split(base64_encode("$email_plain")).$eol.$eol; ;

//HTML version of message
$body .= "--$boundary\r\n" .
   
"Content-Type: text/html; charset=ISO-8859-1$eol" .
   
"Content-Transfer-Encoding: base64$eol$eol";
$body .= chunk_split(base64_encode("$email_html")).$eol.$eol;

# Finished
$body .= "--".$boundary."--".$eol.$eol;  // finish with two eol's for better security.

//send message
$result = mail($_emailer_to, $_emailer_subject, $body, $headers);

if(
$result) {
echo
"MAIL SENT SUCCESSFULLY. YOU'RE A WINNER!";
} else {
echo
"SOME WEIRD ERROR :S. YOU LOSE";
}

?>

Any comments or suggestions just lemme know here

EDIT:
Updated: Plain and HTML versions can now be defined in the $email_html and $email_plain variables
UPDATED 17/02: USE THIS VERSION! This is much more compatible with different mail services, see notes at top of code for more info

Rep or $NP Appreciated As Always


Please register or log-in into NamePros to hide ads

Last edited by beaver6813 : 02-17-2007 at 02:02 AM.
beaver6813 is offline   Reply With Quote
Old 01-21-2007, 10:17 AM   · #2
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
Adoption Breast Cancer Breast Cancer Cancer Survivorship
Great job beaver, very helpful!
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline   Reply With Quote
Old 01-21-2007, 10:30 AM   · #3
Dan
Buy my domains.
 
Dan's Avatar
 
Name: Dan
Trader Rating: (63)
Join Date: Feb 2006
Posts: 2,800
NP$: 54.00 (Donate)
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
Autism Autism Autism Autism Autism Autism Autism
An easier way to give both parts of the message would be nice.
I never knew you could do that, but I knew you could send HTML.
Dan is offline   Reply With Quote
Old 01-21-2007, 12:10 PM   · #4
beaver6813
NamePros Regular
 
beaver6813's Avatar
 
Name: Sam Cleaver
Location: England
Trader Rating: (11)
Join Date: May 2005
Posts: 327
NP$: 82.00 (Donate)
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
Originally Posted by Dan
An easier way to give both parts of the message would be nice.
I never knew you could do that, but I knew you could send HTML.


Updated script above I'm personally using this to send out confirmation emails and what not so you dont have to ask initially whether they just want plain or HTML. Then after they confirm etc ill give them the option to decide whether to have it in plain or html on its own

I multipart/alternative

Last edited by beaver6813 : 01-21-2007 at 12:14 PM.
beaver6813 is offline   Reply With Quote
Old 02-16-2007, 01:15 PM   · #5
DylanButler
NamePros Regular
 
DylanButler's Avatar
 
Name: Dylan Butler
Location: San Diego, CA
Trader Rating: (38)
Join Date: Jan 2006
Posts: 689
NP$: 0.00 (Donate)
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
Will use this, thanks

edit: also be sure to verify this will work on your server as many large hosting companies (dreamhost included) have been disabling the 4th parameter (headers) on mail().
__________________
-Dylan Butler

Coming soon: examp

Last edited by DylanButler : 02-16-2007 at 01:49 PM.
DylanButler is offline   Reply With Quote
Old 02-16-2007, 01:17 PM   · #6
beaver6813
NamePros Regular
 
beaver6813's Avatar
 
Name: Sam Cleaver
Location: England
Trader Rating: (11)
Join Date: May 2005
Posts: 327
NP$: 82.00 (Donate)
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
Will be updating script soon.. gmail didnt like this version so i rewrote for a script i am currently working on I'll cut out the script and update it asap
beaver6813 is offline   Reply With Quote
Old 02-16-2007, 01:48 PM   · #7
Barrucadu
Formally Mikor.
 
Barrucadu's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,438
NP$: 95.25 (Donate)
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
I was wondering how to do that, well well, the wonders of e-mail headers! lol
__________________
Me | Blog | Last.fm | F@h

archlinux User
Barrucadu is offline   Reply With Quote
Old 02-17-2007, 02:01 AM   · #8
beaver6813
NamePros Regular
 
beaver6813's Avatar
 
Name: Sam Cleaver
Location: England
Trader Rating: (11)
Join Date: May 2005
Posts: 327
NP$: 82.00 (Donate)
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
MAJOR UPDATE! Read updated first post for more info!

Is now compatible with Gmail and other services.


Quote:
edit: also be sure to verify this will work on your server as many large hosting companies (dreamhost included) have been disabling the 4th parameter (headers) on mail().



Well if they disable headers a) it makes it very hard to send out valid emails
b) i can't do anything about it :P having just the to, subject and message field is madness if you want to send out a decent email... they really should just filter out specific variables used for spoofing But.. meh!

Last edited by beaver6813 : 02-17-2007 at 02:52 AM.
beaver6813 is offline   Reply With Quote
Old 02-17-2007, 02:32 AM   · #9
kleszcz
 
kleszcz's Avatar
 
Trader Rating: (232)
Join Date: Jul 2006
Posts: 3,290
NP$: 252.35 (Donate)
kleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond reputekleszcz has a reputation beyond repute
Marrow Donor Program Multiple Sclerosis
Thanks for sharing. Great...
__________________
BQB.com auction: CQAW.com, UJMH.com
LLLL.coms: ALL 2 Premium Letters + U + K
kleszcz is offline   Reply With Quote
Old 02-17-2007, 10:22 AM   · #10
DylanButler
NamePros Regular
 
DylanButler's Avatar
 
Name: Dylan Butler
Location: San Diego, CA
Trader Rating: (38)
Join Date: Jan 2006
Posts: 689
NP$: 0.00 (Donate)
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
Originally Posted by beaver6813

Well if they disable headers a) it makes it very hard to send out valid emails
b) i can't do anything about it :P having just the to, subject and message field is madness if you want to send out a decent email... they really should just filter out specific variables used for spoofing But.. meh!




Ya there's nothing you can do about it. However, hosts are doing it and we will have to account for it nonetheless.
__________________
-Dylan Butler

Coming soon: examp
DylanButler is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


Site Sponsors
Build your NameBrand http://www.mobisitetrader.com/ Get Your Site Linked at LinkedKeywords.com
Advertise your business at NamePros
All times are GMT -7. The time now is 05:54 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0