Link to home
Start Free TrialLog in
Avatar of peter-cooper
peter-cooper

asked on

How can I send attachment with mail

Hello
I have existing mail script that works well. However , I now need to send a file as an attachment and have no idea how to do this. I would be grateful if someone could help with this. Thanks

File to send

$file = fopen("destroyoutput.txt", "w+") or die("Unable to open file!");
	$output="";
	
	$v1 = explode(",", $_POST['sub']);
	foreach ($v1 as $values) {
		
     $output.= $values. "\r\n";
  } 

 fwrite($file,$output);
 fclose($file);

Open in new window


mailer script

       
        $to = "someuser@domain.com";
        $subject = "Request";
        $header = "From: user@domain.com\n";
        $header .= "MIME-Version: 1.0\n";
        $header .= "Content-Type: text/html; charset=ISO-8859-1\n";
        $body = "<html><body>";
        $body .= "<b>" . $user .  " of " .  $customer . " has requested a Box.</b>";
        $body .= "<br />";
        $body .= "Date of request: " . $intakedate;
        $body .= "<br /><br />";
        $body .= "Company: " . $customer;
        $body .= "<br />";
        $body .= "Address: " . $address;
        $body .= "<br />";
        $body .= "Box(es): " . $s; 
        $body .= "<br />";
        $body .= "Number of Box(es): " . $boxrslt;
        $body .= "<br />";
        $body .= "Dept: " . $dept;
        $body .= "<br />";
        $body .= "Requestor: " . $user;
        $body .= "<br />";
        $body .= "Service: " . $service;
        $body .= "<br /><br />";
        $body .= "Please do not reply to this automated message. Thank you for using our service.";
        $body .= "</html></body>";
   
        mail($to,$subject,$body,$header);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Allan Nisbet
Allan Nisbet
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter-cooper
peter-cooper

ASKER

@Allan thanks for reply. However, how do I use the $bodytext? Do I just substitute with $body becuase I need to send all in the body as in my code. Thanks
Thanks allan. Nice script. Just had to use $email->IsHTML(true); and it works great
I recommend that you NOT do this.  Instead use ConstantContact. Email is one of those things that seems like it should be easy -- until you actually try to use it, and then you find that it's a crock of overlapping and contradictory interfaces between competing interests.  The only thing you don't know (and cannot know) is what all those other parties are doing with your messages.  It's a little like driving with impaired vision.  Something might or might not be lurching into your path, and the only thing you don't know is what will happen or when.

If you insist on sending your own email, I recommend that you NOT send attachments, ever.  You will find that many email receivers do not allow attachments because they are attack vectors.  Or if they allow the attachments, they quarantine the attachments in a corporate server, and the attachments never get to the recipient.  A better and more dependable design will store the attachment document on the server and put a link to the attachment in the body of the email.  Not only will the link go through where the attachment may get stripped, but you don't clutter up the client's in-box with unnecessary files.

One of the phenomena you will deal with if you insist on writing your own email application is the user's expectation that email is simple and should work correctly.  This is the appearance of email from the perspective of the casual user.  So when it does not work as expected (missing messages or garbled attachments, long delays, some messages lost, your server gets shut down and branded as a spammer, etc) you will find yourself with an angry mob at the gate and you will be struggling under extreme time pressure to make the email work "the way it's supposed to work."  This is a real time sink and a totally thankless task.  There is no win; there is only lost productivity, fear, darkness, and brown sadness everywhere.  If I had an enemy and wanted to make his life miserable, I would tell everyone that he is the guy who can make the email work really well.

My advice to my friends: Stay out of that hell hole and just offload all of the work to a professional organization like ConstanContact. They have competitors, and you might want to evaluate a few of the popular email services, but in my experience ConstantContact were effective, helpful, embarrassingly inexpensive, and totally worth the very modest cost.  When you've tried to send your own email, as I have, there are almost no words to express the lost time and lost productivity that occur.  When I switched the organization to ConstantContact, all of those burdens were lifted, all of our email worked, and I got my life back.  

And the best part...  ConstantContact is a full time professional organization devoted to getting your email to work.  You can buy this service from the professionals or try to learn everything the professionals already know, so you become this service.  I only had a few dozen clients to share the cost, and I found that the economies of scale favored buy over build.

Best of luck, ~Ray
@Ray Thanks for heads up Ray