473,246 Members | 1,583 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,246 software developers and data experts.

PHP E-mail with file attachment

Hello Guys
would you know about php email with his attachment
Working on linux platform.......................................... .........
The code as below:

mail.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //define the receiver of the email
  3. $to = 'swanandkumar@gmail.com';
  4. //define the subject of the email
  5. $subject = 'Test email with attachment';
  6. //create a boundary string. It must be unique
  7. //so we use the MD5 algorithm to generate a random hash
  8. $random_hash = md5(date('r', time()));
  9. //define the headers we want passed. Note that they are separated with \r\n
  10. $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
  11. //add boundary string and mime type specification
  12. $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  13. //read the atachment file contents into a string,
  14. //encode it with MIME base64,
  15. //and split it into smaller chunks
  16. $attachment = chunk_split(base64_encode(file_get_contents('c:\\info.txt')));
  17. //define the body of the message.
  18. ob_start(); //Turn on output buffering
  19. ?>
  20. --PHP-mixed-<?php echo $random_hash; ?>
  21. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  22.  
  23. --PHP-alt-<?php echo $random_hash; ?>
  24. Content-Type: text/plain; charset="iso-8859-1"
  25. Content-Transfer-Encoding: 7bit
  26.  
  27. Hello World!!!
  28. This is simple text email message.
  29.  
  30. --PHP-alt-<?php echo $random_hash; ?>
  31. Content-Type: text/html; charset="iso-8859-1"
  32. Content-Transfer-Encoding: 7bit
  33.  
  34. <h2>Hello World!</h2>
  35. <p>This is something with <b>HTML</b> formatting.</p>
  36.  
  37. --PHP-alt-<?php echo $random_hash; ?>--
  38.  
  39. --PHP-mixed-<?php echo $random_hash; ?>
  40. Content-Type: application/zip; name="attachment.zip"
  41. Content-Transfer-Encoding: base64
  42. Content-Disposition: attachment
  43.  
  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.  
Aug 16 '07 #1
1 2963
Atli
5,058 Expert 4TB
Hi.

Please post your code within [code] tags.

As for your problem.
Check out PHPMailer.
A tutorial on how to use attachments with PHPMailer can be found here.
Aug 17 '07 #2

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

Similar topics

2
by: Gordon H. | last post by:
I'm trying to email a HTML link to a HTML file attached in the same message. I DO NOT want to have the attached HTML file displayed in the message, I just want a link in the email to the HTML file...
1
by: Mike | last post by:
How can I provide a dialog box for a user to select a local file attachment (with just the path to the file stored as a text string in the field in my db). Is there an ActiveX control I should...
1
by: Sam | last post by:
Hi, I want to be able to send an email w/ attachment. I have an INPUT Type="File" HTMLControl on the page. User selects the attachment using this control. That's where I'm stuck. First, I...
1
by: Trapulo | last post by:
How can I send an email with attachments without save the file on the file system? With MailMessage I can only attach a file from file system, but I have the file as byte array and I wan't...
1
by: Second Chance | last post by:
I have an application in php/html/javascript, part of which has to generate an email with attachment. I have done this via php, but now my customer wants the email to appear in Outlook, (so he can...
9
by: deepaks85 | last post by:
Dear Sir, I have created a simple request form which will be mailed to me. Now I want to attach files and send it through that request form. For this I am using the following script: ...
0
by: prasenjit2007 | last post by:
I have a main form for inputing the (to/from/mesg/file) with the following code:- <html> <body> <table> <tr> <td>To:</td> <td><input type="text" name="to" size="50" ...
4
by: PleaseHelpMe | last post by:
Any1 can tell me how to go about sending email with attachment in vb programming code. I know SMTP is one option, but I can't seem to get it working. Any other suggestion?? Thanks in advance
2
by: Erik Witkop | last post by:
So I have been trying to get this to work all day. I can't get a local file on my web server to attach to an email. Right now I have it printing out in the body of the email. Please help me with...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.