ingeva Posted January 7, 2009 Share Posted January 7, 2009 I have read all I could find about sending email attachments, but it seems that whatever I do the received email is blank. If I save it to a file all the information is still there. I want to send a simple e-mail with a .csv attachment. This is also pure text, no special encoding. If I send an "identical" mail from my mail client, everything is fine. If I make a slight adjustment in this file, for instance I change only one character in the mime boundary (all of them), the same thing happens, so I suspect that there is something about making the boundary that I have not understood (and no example explains it). Here's the php script that I now use for testing. Only the "$to" values need to be set for testing. Can anyone see what's wrong here, and inform me what I have to change? Thanks! Here's the code: <?php // ************** Sends the member list. ************* $to = "$name <$email1>"; $charset = "utf-8"; $subject = "Sending email with attachment"; $boundary = "----".md5(time().time()); $Lhead = <<<nn From: Inge Vabekk <inge@secretdomain.com> MIME-Version: 1.0 Subject: Testing Content-Type: multipart/mixed; boundary="$boundary" nn; $body = <<<mm This is a multi-part message in MIME format. $boundary Content-Type: text/plain; charset="$charset"; format=flowed Content-Transfer-Encoding: 8bit Testing mail with attachment $boundary Content-Type: text/csv; name="mlist.csv" Content-Transfer-Encoding: 8bit Content-Disposition: inline; filename="mlist.csv" Navn,Sted,Hjemme,Jobb,Mobil,Stemme Inge Vabekk,1338 Sandvika, 456456456,99,123 Anne Lise,1341 SLEPENDEN,6756 6609,2203 3239,9504 6588, Anne Swang,3440 Røyken,,,, 3 medlemmer totalt. $boundary-- mm; if (mail ($to, $subject, $body, $Lhead)) echo "Mail sent to <$email1>.<br />\n"; else echo "Could not send mail!<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/ Share on other sites More sharing options...
hobeau Posted January 7, 2009 Share Posted January 7, 2009 Hey ingeva, Try http://www.swiftmailer.org/ and save yourself alot of hassle. Really great library especially for sending email attachments. Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-731531 Share on other sites More sharing options...
chronister Posted January 7, 2009 Share Posted January 7, 2009 I like PHPMailer. It is a very simple to use email class that I discovered when trying to do the same thing your doing. I fought for hours trying to get the attachment to send properly, and could not get a non-corrupt attatchment. Within minutes phpMailer was able to do it first shot. Nate Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-731538 Share on other sites More sharing options...
ingeva Posted January 8, 2009 Author Share Posted January 8, 2009 Quote Try www.swiftmailer.org and save yourself alot of hassle. Really great library especially for sending email attachments. Thanks, I'll probably look into that anyway, but since I've tried to copy the exact method from examples I have found, I thought there would be only a minor error; something I had overlooked. I hoped I would have a solution by today, and I believe it would take me more time to study and install this new library, than to find and eliminate the error. It would be a great advantage if I could install the library on my own computer also, so I had a better way of testing my mail scripts. I run Ubuntu Linux. Tips? Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-732777 Share on other sites More sharing options...
chronister Posted January 8, 2009 Share Posted January 8, 2009 There is nothing to install, it is a class that you simply include the class file and use it. here is an example of the phpmailer class. The documentation is very good and adding an attachment is a matter of adding another line below. <?php // prep the email, and call the class include_once('class.phpmailer.php'); $mail = new PHPMailer(); $altBody .='this is the plain text email'."\r\n"; $body .= 'This is <b>HTML</b> email'."\r\n"; // set up email $body = eregi_replace("[\]",'',$body); $mail->From = 'test@yourdomain.com'; $mail->FromName = 'Test User'; $mail->Subject = 'PHPMailer Class Example'; $mail->AltBody = $altBody; $mail->MsgHTML($body); $mail->AddAddress('to_someone@yourdomain.com', 'Bob Smith'); if(!$mail->Send()) { } else {echo '<div class="message">Message sent</div>';} ?> I "learned" how to use this in about 30 minutes. It was very easy to use. So it is your choice on what you want to do. Hope it helps ya. Nate Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-732787 Share on other sites More sharing options...
ingeva Posted January 8, 2009 Author Share Posted January 8, 2009 Quote There is nothing to install, it is a class that you simply include the class file and use it. OK, I'll look into that also. I would need months to find out how to use the swiftmailer, so I feel that we're trying to fix a small problem using a very big gun. I don't know how to use classes; have never used them, and I really don't have much time to learn about them. I need a solution, the faster the better. I don't understand how installing another package would make things simpler. Adding more software is adding another level of complexity. How can I do it as simple as what I have already done -- or simpler? I don't need a volume mailer, or anything sophisticated. It will be used more or less sporadically, like once or twice a week, max. about 40 mails each time. My mails are OK as long as they don't have an attachment. Then they appear blank to the receiver, even if they are not. It only shows if the mail is saved and viewed in text mode. There's evidently just a format error, like in the header or in the mime boundary. Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-732842 Share on other sites More sharing options...
chronister Posted January 8, 2009 Share Posted January 8, 2009 your not adding software. It is a simple php class. It is a collection of functions that handle the tasks of sending mail. All you have to do is download the file, upload it to your server, then use the code I gave you as the basis of your mail. You can send a mail with attachment in less than 20 minutes from the time you read this. Download the class http://phpmailer.codeworxtech.com/index.php?pg=sf&p=dl, then take the code I gave you above, and add this line to add the attachment. $mail->AddAttachment("/path/to/file.zip", "new_name.zip"); You will find it VERY easy to use. Nate Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-732946 Share on other sites More sharing options...
ingeva Posted January 9, 2009 Author Share Posted January 9, 2009 Quote $mail->AddAttachment("/path/to/file.zip", "new_name.zip"); OK, thanks, I'll have to look into that. My attachment is not on file, however. It's just a long string. Should be able to use an inline attachment. Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733148 Share on other sites More sharing options...
chronister Posted January 9, 2009 Share Posted January 9, 2009 I honestly don't know what an inline attachment is. Does it mean that the text is part of the body of the email? If so, then you would just make that part of the $body variable. You could always write it to a temp file, attach the file, send the email and then delete the file when done. Nate Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733546 Share on other sites More sharing options...
ingeva Posted January 9, 2009 Author Share Posted January 9, 2009 Quote You could always write it to a temp file, attach the file, send the email and then delete the file when done. Evidently I could do that but it should be unnecessary. In fact, all attachments are "inline" since they are only separated from each other and the body by a mime boundary. Anyway, I found the AddStringAttachment function which ought to do the trick. However, after a day's work I'm not even where I started. This is going from bad to worse. No error messages, but no mail, not even one that my mail clients made invisible (I use Thunderbird, but Outlook Express acts the same way). Here's my code with phpmailer. Can someone see an obvious error? <?php // ************** Sends the member list. ************* include_once "class.phpmailer.php"; $charset = "utf-8"; $mail = new PHPMailer(); // defaults to using php "mail()" $body = "Testing mail with attachment\n"; $mail->AddAddress($email1, $name); $mail->From = "$MyMail@$domain"; $mail->FromName = "Inge Vabekk"; $mail->Subject = "Sending email with attachment"; $string = "Navn,Sted,Hjemme,Jobb,Mobil,Stemme Inge Vabekk,1338 Sandvika, 456456456,99,123 Anne Lise,1341 SLEPENDEN,6756 6609,2203 3239,9504 6588, Anne Swang,3440 Røyken,,,, 3 medlemmer totalt. "; $mail->AddStringAttachment($string, "mlist.csv", $encoding ="$charset", $type = "text/csv"); if(!$mail->Send()) echo "Mail sent to <$email1>.<br />\n"; else echo "Could not send mail!<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733561 Share on other sites More sharing options...
chronister Posted January 9, 2009 Share Posted January 9, 2009 I got it to work on my server and send me an email with a proper attachment. Change the following lines $mail->AddStringAttachment($string, "mlist.csv", $encoding ="$charset", $type = "text/csv"); if(!$mail->Send()) echo "Mail sent to <$email1>.<br />\n"; else echo "Could not send mail!<br />\n"; To $mail->AddStringAttachment($string, "mlist.csv"); if(!$mail->Send()) echo "Could not send mail!<br />\n"; else echo "Mail sent to <$email1>.<br />\n"; Change these 2 items and it should work for you as it did for me. Nate Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733588 Share on other sites More sharing options...
ingeva Posted January 10, 2009 Author Share Posted January 10, 2009 Tried that. Result: Could not send mail! Any more suggestions? This should really be quite simple, and adding a complicated library like this doesn't seem to be the right thing to do. I don't understand how it works, and I hate the idea of having to debug unknown code (and php's debugging is lousy!!!) in order to find a minor error like this. Because it IS a minor error, since only a minor change changes a working e-mail to a non-working. However, even if I change a previously working mail back to the original, it won't work!!!! It looks as if someone or something is out to get me. It's a long time since I've had this kind of trouble for such a minor thing. Before I scrap it altogether I'll try phpmailer support. Thanks for trying! Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733887 Share on other sites More sharing options...
ingeva Posted January 10, 2009 Author Share Posted January 10, 2009 Quote Tried that. Result: Could not send mail! This error appeared because I had reversed the error test. The mail was actually sent, but without the body text. I changed my script thus: <?php // ************** Sends the member list. ************* include_once "class.phpmailer.php"; $CharSet = "utf-8"; $mail = new PHPMailer(); // defaults to using php "mail()" $mail->AddAddress($email1, $name); $mail->From = "$MyMail@$domain"; $mail->FromName = "Inge Vabekk"; $mail->Subject = "Sending email with attachment"; $mail->Body = "Testing mail with attachment\n"; $string = "Navn,Sted,Hjemme,Jobb,Mobil,Stemme Inge Vabekk,1338 Sandvika, 456456456,99,123 Anne Lise,1341 SLEPENDEN,6756 6609,2203 3239,9504 6588, Anne Swang,3440 Røyken,,,, 3 medlemmer totalt. "; $mail->AddStringAttachment($string, "mlist.csv"); // , $charset, "text/csv"); if($mail->Send()) echo "Mail sent to <$email1>.<br />\n"; else echo "Could not send mail!<br />\n"; ?> Note the $CharSet above. Although I set the variable name to what was defined in the class as "public $CharSet", the body text still came out with the iso-8859-1 character set. This is not good since MS software doesn't interpret it correctly. We could live with it for this one mail since it would hardly contain "strange" characters, but if you know how to change it I would be happy, and we could use phpmailer for other purposes as well (although I still think it's an unnecessarily complicated solution)! If I can find out how, I'll mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733906 Share on other sites More sharing options...
ingeva Posted January 10, 2009 Author Share Posted January 10, 2009 GOT IT! The simplest is always the best. PHPmailer gone. The clue was that I had let the leading hyphens be a part of the mime boundary, instead of Inserting them before it. I now get the body text in utf-8 and the attachment in the message (attachment inline) and also readily available to save or open separately. I have removed an element of uncertainty, and I have learnt a lot. Thank you for all your help! Without it I might have given up, but I'm not an easy quitter. PHPmailer and SwiftMail still look interesting enough, so I'll probably look into them again when it becomes relevant, for instance if I need smtp send. Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-733915 Share on other sites More sharing options...
Jamied_uk Posted December 3, 2013 Share Posted December 3, 2013 (edited) can you post the working code as im getting no file attached i can post my code if it helps but its a static file and named the same file in the same place all the time but it sends email but the attached file is missing from the email and i have no idea why i have tryed so many different ways, i wish to not use phpmailer or swift i just want to use php if at all posible please help. $random_hash = md5(date('r', time())); $to = "$e"; $from = "admin@jnetscripts.com"; $subject = 'J~Net SSL Certificate Notification!'; $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>J~Net Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.jnetscripts.com/premium/ssl_cirtificate.zip"><img src="http://www.jnetscripts.com/images/logojnet.png" width="36" height="30" alt="Click To Visit" style="border:none; float:left;"></a> Hi there, You have a new Balance on J~Net </div><div style="padding:24px; font-size:17px;"><br /><br />Hi '.$username.' Heres your SSL Certificates You Requested! Any Questions or issues please Contact Multimedia @ J~Net <p> <a href="http://www.jnetscripts.com/multimedia</a><br /></div></body></html>'; $headers = "From: $from\n"; $header .= "Content-Type: multipart/mixed\n\n"; $header .= "name=\"ssl_cirtificate.zip\"\n\n"; $header .= "Content-Transfer-Encoding: base64\n\n"; $header .= "Content-Type: application/zip; name=\"ssl_cirtificate.zip\"\n\n"; $header .= "--$random_hash--"; Edited December 3, 2013 by Jamied_uk Quote Link to comment https://forums.phpfreaks.com/topic/139835-solved-simple-email-attachment/#findComment-1461028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.