PHP sending a simple email with a txt document attachment

Posted on 2011-09-26
Last Modified: 2012-05-12
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...
 $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

Question by:funkkube
  • 3
  • 3
  • 2
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36700037
You can use phpmailer library to send mails with attachment.

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

just ref.

Author Comment

ID: 36707500
i have this working but it keeps putting the number 1 on the page when it loads up..
any idea why?
  $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 = "
Content-Type: multipart/alternative; boundary='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.
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>
Content-Type: text/plain; name=subscriberse.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment

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

Open in new window


Author Comment

ID: 36707544
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..
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36707557
just download the class library and include on your mail process file.
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 36713930
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.

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

Good luck, ~Ray

Author Comment

ID: 36815607
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?
LVL 108

Expert Comment

by:Ray Paseur
ID: 36891040
... 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.

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
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36891184
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now