Jump to content

[SOLVED] Simple email attachment


ingeva

Recommended Posts

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";

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

  • 4 years later...

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 by Jamied_uk
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.