• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

PHP sending a simple email with a txt document attachment

Hi
I need to send a simple text file as an attachment to an email..
How can I do it?
The text file is on the server...
cheers
mat
<?php
 $to = "matxxxx";
 $subject = "Someone has subscribed to ALICE.. coz ur great.. :-)";
 $body = "Another fan who loves your musak - {$_POST['txtUsername']}";
 if(mail($to, $subject, $body)) echo("<p>Thanks for subscribing! </p>");
 else echo("<p>.</p>");
?>

Open in new window

0
funkkube
Asked:
funkkube
  • 3
  • 3
  • 2
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
You can use phpmailer library to send mails with attachment.

You need to give the path to attach while you send.

just ref.
http://phpmailer.worxware.com/
http://phpmailer.worxware.com/index.php?pg=tutorial
0
 
funkkubeAuthor Commented:
thanks
i have this working but it keeps putting the number 1 on the page when it loads up..
any idea why?
cheers
<?php
 
  $to = "matxxx";
 
  $subject = "Latest SUBSCRIBERS list.. Download, Open, Apple-A, Apple-C, then Apple-V in the TO box in GMAIL";
  
  $random_hash = md5(date('r', time()));
 
  $headers = "From: matxxxx\r\nReply-To: matxxx";
 
  $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
 
  $attachment = chunk_split(base64_encode(file_get_contents("subscribers/subscriberse.txt")));
 
  $output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
 
Hello World!
This is the simple text version of the email message.
 
--PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>
 
--PHP-alt-$random_hash--
 
--PHP-mixed-$random_hash
Content-Type: text/plain; name=subscriberse.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment
 
$attachment
--PHP-mixed-$random_hash--"

;
 
  echo @mail($to, $subject, $output, $headers);
 
?>

Open in new window

0
 
funkkubeAuthor Commented:
how do i install the php file that does the work?
I have it in my root on the site?
changed the  REQUIRE once parameter..
im a noob..
ta
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Loganathan NatarajanLAMP DeveloperCommented:
just download the class library and include on your mail process file.
0
 
Ray PaseurCommented:
Email is not simple at all -- there are many moving parts that are not under your control, and if you're a "noob" there are better places to cut your teeth.  But you can learn some of it by reading this man page in painstaking detail.  Follow the links to the RFCs and other pages and make sure you understand them, too.  Read every one of the user-contributed notes.
http://php.net/manual/en/function.mail.php

When you have read all of that, consider using a professional Software-As-A-Service offering like ConstantContact.com

Good luck, ~Ray
0
 
funkkubeAuthor Commented:
Thanks I used phpmailer in the end and just included the code in the page itself..
A little clunky I guess but I couldnt get it to find the location of the php file as i guess the paths were a little wrong.
in some of the documentation it states having to put it in the php library which as far as i know I do not have access to.
i tried putting it in the root of the website but still no joy.
the site is hosted on a oneandone server if that shines any light..
im sure the path thing was the issue but i couldnt work it out.
sticking the contents of the phpmailer file in the webpage at the beginning and then removing the RUNONCE command did the job and it works like a charm.
Would there be any issues with security doing this?
Cheers
Mat
0
 
Ray PaseurCommented:
... any issues with security?

Security is not an absolute, it is a continuum and is mostly predicated on what you are trying to protect.  Nuclear launch codes, financial data, medical records, bowling scores - all have security requirements that differ.

Here is the mantra: Filter input, escape output.  If you use any external data at all in your programming, you have a security exposure to tainted data and an attack vector.  To reduce the risk from this exposure you can use the PHP filter_var() function variants.
http://php.net/manual/en/function.filter-var.php

Maybe it is not part of the issue any more, but I saw this line in an earlier code snippet:
echo @mail($to, $subject, $output, $headers);

I doubt if that is something you really want :-)

Best regards, ~Ray
0
 
Loganathan NatarajanLAMP DeveloperCommented:
A little clunky I guess but I couldnt get it to find the location of the php file as i guess the paths were a little wrong.

You can very well find the path of php file... just ref these,

sample 1

sample 2
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now