473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

strange characters when trying to attach the file to mail

I have a strange issue, i have a doc file on the server with the following text in:

Thank u....

I am using the following to attach the document to my mail:
Expand|Select|Wrap|Line Numbers
  1. <!-- Mail Attachment -->
  2. <?php
  3.  if(isset($_POST['sub']))
  4.  { 
  5.       //define the receiver of the email 
  6.     $to = $_POST['mailid']; 
  7.     //define the subject of the email 
  8.     $subject = 'Test email with attachment'; 
  9.     //create a boundary string. It must be unique 
  10.     //so we use the MD5 algorithm to generate a random hash 
  11.     $random_hash = md5(date('r', time())); 
  12.     //define the headers we want passed. Note that they are separated with \r\n 
  13.     $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
  14.     //add boundary string and mime type specification 
  15.     $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
  16.     //read the atachment file contents into a string,
  17.     //encode it with MIME base64,
  18.     //and split it into smaller chunks
  19.                         //$attachments=file_get_contents('/var/chroot/home/content/67/7399467/html/confirmation.zip');
  20.     $attachment = chunk_split(base64_encode(file_get_contents('/var/chroot/home/content/67/7399467/html/confirmation.zip'))); 
  21.         echo $attachments;
  22.     //define the body of the message. 
  23.     ob_start(); //Turn on output buffering 
  24. ?> 
  25.     --PHP-mixed-<?php echo $random_hash; ?>  
  26.     Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
  27.         --PHP-alt-<?php echo $random_hash; ?>  
  28.     Content-Type: text/plain; charset="iso-8859-1" 
  29.     Content-Transfer-Encoding: 7bit
  30.            Hello World!!! 
  31.        This is simple text email message. 
  32.  
  33.     --PHP-alt-<?php echo $random_hash; ?>  
  34.     Content-Type: text/html; charset="iso-8859-1" 
  35.     Content-Transfer-Encoding: 7bit
  36.            <h2>Hello World!</h2> 
  37.        <p>This is something with <b>HTML</b>   formatting.</p> 
  38.  
  39.     --PHP-alt-<?php echo $random_hash; ?>-- 
  40.         --PHP-mixed-<?php echo $random_hash; ?>  
  41.     Content-Type: application/zip; name="attachment.zip"  
  42.     Content-Transfer-Encoding: base64  
  43.     Content-Disposition: attachment  
  44.         <?php echo $attachment; ?> 
  45.                                 --PHP-mixed-<?php echo $random_hash; ?>-- 
  46.  
  47. <?php 
  48.                                 //copy current buffer contents into $message variable and delete current output buffer 
  49.                                 $message = ob_get_clean(); 
  50.     //send the email 
  51.     $mail_sent = @mail( $to, $subject, $message, $headers ); 
  52.     //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
  53.     echo $mail_sent ? "Mail sent" : "Mail failed"; 
  54.   }
  55. ?>

When i try to echo $attachments.i get the strange characters in my output screen.I did so,because my mail is
sending with no attachment and text(nomane 0k as attachment file) and no text in body.
May 24 '12 #1

✓ answered by NVijaya

I myself solved the issue.This might help others.


Expand|Select|Wrap|Line Numbers
  1. $to = $_POST['mailid'];
  2.                                             $from = "VySystems <vijaya@vysystems.com>";
  3.                                             $subject = $_POST['uname']."testing";
  4.  
  5.                                             $separator = md5(time());
  6.  
  7.                                             // carriage return type (we use a PHP end of line constant)
  8.                                             $eol = PHP_EOL;
  9.  
  10.                                             // attachment name
  11.                                             $filename = "ip.zip";
  12.  
  13.                                             //$pdfdoc is PDF generated by FPDF
  14.                                             $attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
  15.  
  16.                                             // main header
  17.                                             $headers  = "From: ".$from.$eol;
  18.                                             $headers .= "MIME-Version: 1.0".$eol; 
  19.                                             $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
  20.  
  21.                                             // no more headers after this, we start the body! //
  22.  
  23.                                             $body = "--".$separator.$eol;
  24.                                             $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  25.                                             $body .= "This is a MIME encoded message.".$eol;
  26.  
  27.                                             // message
  28.                                             $body .= "--".$separator.$eol;
  29.                                             $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
  30.                                             $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  31.                                             $body .= $message.$eol;
  32.  
  33.                                             // attachment
  34.                                             $body .= "--".$separator.$eol;
  35.                                             $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
  36.                                             $body .= "Content-Transfer-Encoding: base64".$eol;
  37.                                             $body .= "Content-Disposition: attachment".$eol.$eol;
  38.                                             $body .= $attachment.$eol;
  39.                                             $body .= "--".$separator."--";
  40.  
  41.                                             // send message
  42.                                             if (mail($to, $subject, $body, $headers)) {
  43.                                                 echo "mail send ... OK";
  44.                                             } else {
  45.                                                 echo "mail send ... ERROR";
  46.                                             }

1 2446
I myself solved the issue.This might help others.


Expand|Select|Wrap|Line Numbers
  1. $to = $_POST['mailid'];
  2.                                             $from = "VySystems <vijaya@vysystems.com>";
  3.                                             $subject = $_POST['uname']."testing";
  4.  
  5.                                             $separator = md5(time());
  6.  
  7.                                             // carriage return type (we use a PHP end of line constant)
  8.                                             $eol = PHP_EOL;
  9.  
  10.                                             // attachment name
  11.                                             $filename = "ip.zip";
  12.  
  13.                                             //$pdfdoc is PDF generated by FPDF
  14.                                             $attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
  15.  
  16.                                             // main header
  17.                                             $headers  = "From: ".$from.$eol;
  18.                                             $headers .= "MIME-Version: 1.0".$eol; 
  19.                                             $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
  20.  
  21.                                             // no more headers after this, we start the body! //
  22.  
  23.                                             $body = "--".$separator.$eol;
  24.                                             $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  25.                                             $body .= "This is a MIME encoded message.".$eol;
  26.  
  27.                                             // message
  28.                                             $body .= "--".$separator.$eol;
  29.                                             $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
  30.                                             $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  31.                                             $body .= $message.$eol;
  32.  
  33.                                             // attachment
  34.                                             $body .= "--".$separator.$eol;
  35.                                             $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
  36.                                             $body .= "Content-Transfer-Encoding: base64".$eol;
  37.                                             $body .= "Content-Disposition: attachment".$eol.$eol;
  38.                                             $body .= $attachment.$eol;
  39.                                             $body .= "--".$separator."--";
  40.  
  41.                                             // send message
  42.                                             if (mail($to, $subject, $body, $headers)) {
  43.                                                 echo "mail send ... OK";
  44.                                             } else {
  45.                                                 echo "mail send ... ERROR";
  46.                                             }
May 25 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Buck Turgidson | last post by:
Not sure if this is a php or Apache2 question, but I get strange characters when using phpman to generate linux man pages, as shown below. Using another server, I don't have the problem. Could...
1
by: Roman Muntyanu | last post by:
Hi all ! After I run resgen my.fr-CA.txt my.fr-CA.resx I got my.fr-CA.resx without any french characters, they were cut at all. How to let resgen know about language specific encoding ? ...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
3
by: Richard Marsden | last post by:
Here's another strange error I'm getting when using ODBC to access MySQL. This time ODBC is being more informative, although all the documentation I've looked at, claims that the function in...
2
by: laredotornado | last post by:
Hi, Using PHP 4, I'm trying to create a class, and I get warnings in my constructor function like the one below: Notice: Undefined variable: m_cat_to_buy in...
2
by: John Salerno | last post by:
If I read a string that contains a newline character(s) into a variable, then write that variable to a file, how can I retain those newline characters so that the string remains on one line rather...
2
by: Curtis | last post by:
Hello everyone: I have come to love the ease of updating PHP, since getting used to using it these past few years. Recently, however, when I upgraded from PHP 5.1 to PHP 5.2.0 and again when...
1
by: Sinister747 | last post by:
Hi All, I am using FCKEditor to allow a client to input a news article.. and the output is saved in MS Access. However when i output the record i get a "Â" character randomly positioned in the...
2
by: danalovesc | last post by:
Hi all, i'm new to Templates and when i tried to implement templates methods which needs to use a map (which is a member in private), the map can only do 3 things: insert, swap, and operator =. and i...
9
by: adamjblakey | last post by:
Hi, I have a strange issue, i have a txt file on the server with the following text in: In what ways has 'time-space compression' transformed geographies of consumption and production? I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.