Solved

php email problem -  html / plain text with attachment

Posted on 2009-04-06
2
1,251 Views
Last Modified: 2012-06-21
Hi , I am having some problem with my php mail scripting.
 
I want an email with HTML and plain text version to be displayed according to the email client - if it has html options on, use html version other wise use text version.

The script works fine with my outlook - which picks up the html version and ignores the text version.

 but in my other online mail client which doesnt have html render turned on, it displays everything from the ob_start(); as plain txt, also my attachement (a pdf file) was displayed as encoding as well.

Is it a problem with my php version on the server? I m currenlty having php 4.3 installed.

Any help will be appreciated.
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$producter=$_POST['product'];
$message=$_POST['message'];
 
//define the receiver of the email 
$to = $email;
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: admin@poolexpress.com.au\r\nReply-To: admin@poolexpress.com.au"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('Zodiac_Tri_Web.pdf'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 
 
--PHP-alt-<?php echo $random_hash; ?>-- 
 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: pdf; name="Zodiac_Tri_Web.pdf"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  
 
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
 
<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ) or die ("Mail failed."); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
?>

Open in new window

problem-with-plainTXT.png
0
Comment
Question by:arthur_raft2
2 Comments
 
LVL 3

Accepted Solution

by:
ecomm earned 500 total points
ID: 24083864
Hi,

I would strongly suggest using the PHPMailer class which is available at http://phpmailer.sf.net 
PHPMailer will handle encoding for the text and Html versions if they exist, along with one or more attachments. It has many other benefits as well. I've used it for 6 years with great success.

Spencer
0
 
LVL 10

Expert Comment

by:wildzero
ID: 24086227
I agree, PHPMailer is excellent.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question