otuatail Posted November 10, 2009 Share Posted November 10, 2009 Hi all. I have a problem with emails. I am trying to set up a mail system where customers can subscribe to a news letter . Those who subscribe will receive a monthly email. They can unsubscribe using a link in the email. The website I have created is for a charity in Malawi. What would be cool is if they could send an attachment in the form of a pdf. The mail() function doesn’t allow for attachments. mail($to,$subject, $message, $headers, $root) Is there another way to send mail with attachments? The problem is that I pay a hosting company. I have no physical access to the computer and can’t install any software. Is this still possible or will I have to make do with just text based emailing. TIA Desmond. Quote Link to comment Share on other sites More sharing options...
gevans Posted November 10, 2009 Share Posted November 10, 2009 A very easy solution would be a to have a download link in the email. Instead of attaching the pdf and sending a lot of data to a lot of people, just let them download it if they want it. Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 The mail function does allow you to have attachments. If by doesn't you mean it doesn't have a handy parameter you pass in of the filename and it does all the work for you, then you are correct, but it doesn't mean you can't use attachments. First result in google... http://www.webcheatsheet.com/php/send_email_text_html_attachment.php Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 Hi Thanks I tried this with a pdf file but when I get the email the attachment is Testemail.txt (517.7k) I can't find Testemail.txt in the code. <?php //define the receiver of the email $to = 'otuatail@aol.com'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('August.pdf'))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers, '-froot@des-otoole.co.uk' ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> and this html? --PHP-mixed- ?? Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 You have set the content type of the attachment to zip and called it attachment.zip, which is obviously not right. Not entirely sure why you got it called testemail.txt, is testemail is the name of your script? Quote Link to comment Share on other sites More sharing options...
chauffeur Posted November 10, 2009 Share Posted November 10, 2009 can your servers use and allow use of perl or cgi-scripting? Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 No. In the example it was a zip file. I changed the attachment to a valid pdf file. Which is what I was intending the attachments to be. It seems I have to make the changes every where. can your servers use and allow use of perl or cgi-scripting? Can I check this out in phpinfo()? I see PCRE (Perl Compatible Regular Expressions) Support enabled Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 Of course you have to change it everywhere, you can't just copy an example and expect it to work when you change the filename, you need to amend mimetypes etc to make it correct for the attachment you wish to use. Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 My ptest page is MailAttach.php so wherethe attchment Testemai.txt comes from ? Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 Looks like it's down to the way your host handless malformed e-mails. I tested your code and the example on the site, changing the mime-types etc and all relevant data. Your code is malformed so you have removed/changed something to break it, the code from the site works fine. You just need to change the mime type in the attachment section to application/pdf, and change the filename. <?php //define the receiver of the email $to = 'your e-mail'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('example.pdf'))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/pdf; name="example.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 Mail failed Report to moderator Logged The only changes I made to your above code is add , '-froot@des-otoole.co.uk' which fails with or without it. I renamed the pdf file on the server to example.pdf which is the same location as the php file. Desmond. Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 So did you change the $to address? Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 Ok now changed to $to = 'otuatail@aol.com'; Result: Mail sent Report to moderator Logged Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 So the mail sent... Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 Not exactly. Still have the strange txt attachment. What I get in the body of the email is this. [Only the first part of this message is displayed. The entire message has been turned into a text attachment, which you can retrieve by clicking the Download button. Once downloaded, open the file with a word processor or text editor and you will be able to read it.] --PHP-alt-0ba24989b762be816ee84194600a234aContent-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-0ba24989b762be816ee84194600a234aContent-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World! This is something with HTML formatting. --PHP-alt-0ba24989b762be816ee84194600a234a-- --PHP-mixed-0ba24989b762be816ee84194600a234aContent-Type: application/pdf; name="example.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment JVBERi0xLjMKJeLjz9MKNSAwIG9iago8PAovTGVuZ3RoIDcyODEKL0ZpbHRlciAvRmxhdGVEZWNv ZGUKPj4Kc3RyZWFtCnic1V1rr9zGeYYuluQjwYeibcmWc1b0LdljW2tehre4t6QpChT9EkNAPlj9 lDZJC8uF1f8PdGa45DzvcB5yeHaVohaQ7Nkdcobv9XkvM/w5yw9FmeXm3/jhj68vvv2+zf78Pxc/ X6j+0PdNkTV9pQ5Nm72+aJX85kf3TdcWwzfTVdM3f7n4Q/bThZ0m+/6fjx/e/FnP0B0q85/9Aj// 8XX225d6IV3WHJq+zbvs5Z8ucn3fXpWtHVFktfmzrrKubA5lV2QvX1/ss+uX/3XxTy8vfq///ZwV uRoGK1Vl07JUb5dlH7TIs9/9tx57k7VpghWhdRWaNF2XtV136JRd1g/7W9f5ocz7smn3t83HolXF /s5d/bHoy74r9++Yb2uV1+X+nvlY1rX+eF9/zKu6K9tu/+D6haZF0VXd/t3rF/rCrm7qav/qJ/NH WTaVvvdDc8Om0FeLMXDzR/brotL32b96Y25fl/rPcv+eu/2dS3ubvmo6M2b6frpNv0/MiK7rCzr/ w+vm0FVFW8oRw9N3Zan2jwN3LvW6m4Pq9D8c+o7+si9VrWBipE0KM+D378Pc5i7/9vJfLlRzqFTT ZC/K4tA32ct/1+z5YBhVln29/9AsS+mnb/fvw/OkZl1KFSUOQCLj5+OQrq73d4BqT6wUFH3V4xM/ 1Xeuuz5vNA8cPz4ChgTZWuA94OPHdkRZFmr/LPjxE/dxoMiLpjy0PVJE/6D1SIu4ygp1yJWT8qo5 SnnZ6Wv6OmvL/lA3KOZK5YVhtJlF073SNDDPpTRP9IPfNd/nvVKtXbYZ1Be5fsh7192hKc23t6/r XB2qXpnF6UU0WW/XdVxD1Y1r6KpD187X8MJSsVRlf1xFrTRR9SrMZz1Wr44OuguDhtVVmjO9vODe QMG8aZUWN/jhwXB11VhBNo/WFn1/1AA9qG87N7f94zhHp42LZrO7+j4OwgWKH+7AFTj3LTYIV36L TXEXLsD13brWCnLI88oKTV0bO9JnL4r80HSDHj169ebVm/fGGzdF3u0v9YKL8qA/NvskEdRChjyI Yc54QdEVctDlkdjaKzli21+0vhTFoavbVtsbPUapEohrlEuQ4QF59Mf2Nn3eG9NnnkfbcuXxFcfj qvEzjrFGK8+1zSlLbWusCao6lXucEX98AKMkw4E1vfY72ct/vXj51Q9Utu8jjxLBMRT7h+ySkEjG M/XIir5uQkpW1HlbGPIYOtddLcZ8aEyM9iudZaOVrFauGW5+y41w3EfxBiXFJ6KqgRKIevXEfs6V Quote Link to comment Share on other sites More sharing options...
chauffeur Posted November 10, 2009 Share Posted November 10, 2009 Hold tight mate. I may still have a mail script in Perl that allows attachments and works well. I'll dig it out asap. Fret not. I'll be back!! Quote Link to comment Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 The current script works perfectly fine for me... Quote Link to comment Share on other sites More sharing options...
bossman Posted November 10, 2009 Share Posted November 10, 2009 that tutorial you found in google in 2 seconds on the first page doesnt help much. Can you point us toward a tutorial that shows you how to pass the uploaded image from FORM into an email? that tutorial doesnt help vary much as it doesnt show how to PASS the file into the process page that sends out the actual email. i'm trying to figure out how to send a zip file as well and i cant find any valuable resources online anywhere. Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 10, 2009 Author Share Posted November 10, 2009 bossman that tutorial you found in google in 2 seconds on the first page doesnt help much Not sure who did this post. ----------------------- chauffeur look forward to your Perl code. -------- I spent some time writing a website in C# (Microsoft.net) a Colleague had to make some alterations on a server to allow difrent file tipes like docx and pdf. I was wondering if some servers do not recognise some types as I think this is what he had to do to the server. I do think that gevans original idea to put a link in the email is good but dammed am i after going this far with you guys that we don't get this solution TIA Desmond. Quote Link to comment 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.