try another color:

 
try another fontsize: tiny small normal big huge
netsperience 2.x
web development, web site design, seo and smo

php

Attach a file to an email with PHP - code that actually works

I am using Open Inviter as a contacts importer, but I need to attach the same file to each email it sends. After Googling for code snippets, and trying out three of them I was able to debug the code so it actually works. The snippets I found all had errors, such as "n" instead of "\n" and "." instead of ".=" - or screwy quotes.

Here is a link to my Open Inviter code snippet - try a demo which sends a file named "download.zip" that contains the Open Inviter files.

And herewith, PHP code that actually works to attach a file to an email, which needs some minor customizations for your specific situation:

$email_to = "sendto@email.com"; // The email you are sending to (example)
$email_from = "sendfrom@email.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt = "text body of message"; // Message that the email has in it
$fileatt = "/home/decibelp/public_html/OpenInviter/download.zip"; // Path to the file (example)
$fileatt_type = "application/zip"; // File Type
$fileatt_name = "download.zip"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to,$email_subject,$email_message,$headers);

if you are using a form to send the email(s) you may use $_POST values:

$email_from = $_POST['email_from']

Learn more about sending email with PHP at W3Schools

Code tested and attachment received by Yahoo, Gmail, and POP mail via Thunderbird. I am amazed at the amount of non-working code that is posted on the internet - please let me know if you have problems with this code working.

jQuery to the Rescue! Drupal warning: Illegal offset type in isset or empty in user.module

Call me old fashioned. I started using JavaScript in the 90s before jQuery was even an idea. Sure, I can troubleshoot jQuery, and it is used a lot in Drupal, but my first impulse is to write regular JavaScript, which still works too. I think jQuery is the best JS framework, I've worked with prototype.js, mootools etc. too.

But when I wanted to hide an obscure and possibly meaningless warning message on the user account edit page (so that new members would not get freaked and think they did something wrong) I struggled.

First I hacked the user.module and although I removed the warning I also gave everybody admin access, including people who are not even logged in - not a good idea.

So I used CSS to hide the warning messages on "body-page.user" which seemed to do the trick.

Then my client told me he was having trouble creating new accounts, and I realized that I hid ALL user module warnings including "User name xxxx already exists" so I was back with the ugly warning.

Finally, I decided to hide just the warning with the text in question. Sure, I could have done it with classic JavaScript, indexing through all the div elements and looking for the matching innerHTML text and hiding the div. I guess that would have been about 8 lines of code.

I decided to figger out the best jQuery method. I came up with one line of jQuery code to add to my theme template that did the trick:

<script type="text/javascript">$("div.messages:contains('Illegal offset type in isset or empty')").hide();</script>

(note: the .messages selector is crucial; without it, all divs that are parents of the warning are also hidden, effectively hiding the entire page!)

jQuery: write less, do morethanks, jQuery...

now about that PHP warning...

learn jQuery at W3Schools

PHP, SEO, and Spiders Google Search Screenshot

PHP, SEO, and Spiders Google Search Screenshot

I was contracted to write an article about PHP and SEO. When I submitted it, I was informed that I was a ghost writer and my name would not appear on the article (another Craigslist story). I decided to give up the credit and take the money. The article has been widely distributed and syndicated on the web.

code snippets widgets and stuff

My projects at The Webmaster Forums

There you can find:

  • Event Links List: scrollable div (no scrollbars) plus admin without database   
  • age verifier
  • check if PHP is parsed in HTML files   
  • password-protected download   
  • basic php email feedback form   
  • Up Against the War   
  • QuizBlender: JS Multiple Choice Quiz Engine and Editor: Common Spanish Phrases Demo   
  • The Ultimate Original JavaScript ColorShow

and more!

the Webmaster Forums

Psycho Therapy Clothing Quiz Screenshot

Psycho Therapy Clothing Quiz Screenshot

Adapted open source php quiz for Psycho Therapy Clothing web site, with randomly generated teaser images of products (tshirts) at the bottom. Themed it with CSS to integrate seamlessly with the rest of the site.

log in with your account on

Syndicate content