Sending an email attachment


For my website, I want to write a feature that will allow me to send through an email attachment some of my hosted files to users.  However, once I have opened and read the file into a variable, i cant seem to actually make the attachment work :(

Can someone help me out?


Who is Participating?
Marcus BointonConnect With a Mentor Commented:
Well, you could do something like:

$mail->AddStringAttachment(file_get_contents($url), 'somefile.txt');

but I wouldn't advise that as it has no scope for error control, and remote links are prone to error. It is worth separating the two parts of the process. Instead, use file_get_contents to grab the remote file contents and save it locally, then pass the path to PHPMailer using a normal attachment:

Marcus BointonCommented:
Don't bother using mail(). Download PHPMailer from, upload it to your web area and call it with a script like the ones here:

You don't need to load the file before sending it, just point PHPMailer at the file and it will grab it for you.
MrBarbyAuthor Commented:
OK, thanks, I'll give that a go :)
MrBarbyAuthor Commented:
Hmm... I just tested this and it didn't seem to work.  Please note that I'm trying to download files that may not necessarily be from the same server, i.e. an http/ftp link.  Should this class still work with that?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.