Email attachments must be present or email will not send.

waffe
waffe used Ask the Experts™
on
I have a script that takes in form data and two files and sends it all out in an email. All of this works as long as you have both files in the tmp directory for the code to add to the email. Can someone add in the code that will allow the email to be sent regardless if there is zero, one or two attached files.

#!/usr/bin/perl

use Fcntl ':flock';
use MIME::Lite;
use Net::SMTP;
use CGI;

my $q = new CGI;
my $name = $q->param("name");
my $email = $q->param("email");
my $phone = $q->param("phone");
my $bandname = $q->param("bandname");
my $genre = $q->param("genre");
my $peoplecount = $q->param("peoplecount");
my $url = $q->param("url");
my $comment = $q->param("comment");
my $dynamicid = $q->param('sendid');
my $mp3 = ".mp3";
my $pdf = ".pdf";

### Adjust sender, recipient and your SMTP mailhost
my $from_address = company@company.com';
my $to_address = 'bill@to.com';
my $mail_host = 'mailhost.is.com';

### Adjust subject and body message
my $subject = $bandname;
my $message_body ="\n Name = $name\n Email = $email\n Phone = $phone\n Bandname = $bandname\n Style = $genre\n People = $peoplecount\n WebSite = $url\n\n Comment = $comment\n";

### Adjust the filenames
my $my_file_gif = "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$mp3";
my $your_file_gif = 'your_file.mp3';
my $my_file_zip = "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$pdf";
my $your_file_zip = 'your_file.pdf';

### Create the multipart container
$msg = MIME::Lite->new (
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text message part
$msg->attach (
  Type => 'TEXT',
  Data => $message_body
) or die "Error adding the text message part: $!\n";

### Add the GIF file
$msg->attach (
   Type => 'audio/mp3',
   Path => $my_file_gif,
   Filename => $your_file_gif,
   Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";

### Add the ZIP file
$msg->attach (
   Type => 'application/pdf',
   Path => $my_file_zip,
   Filename => $your_file_zip,
   Disposition => 'attachment'
) or die "Error adding $file_pdf: $!\n";

### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);


open(IN, "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$mp3");
open(IN2, "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$pdf");
flock(MBOX,LOCK_EX); # exclusive
$msg->send || die "can't send mail--$!";
flock(MBOX,LOCK_UN);
close(IN);
close(IN2);
unlink("/home/www/your.com/company/data_base/booking_tmp/$dynamicid$mp3");
unlink("/home/www/your.com/company/data_base/booking_tmp/$dynamicid$pdf");
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Perl programmer, author and trainer
Commented:
Just check the existance of the files before attaching them.

### Add the GIF file
if (-f $my_file_gif) {
  $msg->attach (
    Type => 'audio/mp3',
    Path => $my_file_gif,
    Filename => $your_file_gif,
    Disposition => 'attachment'
  ) or die "Error adding $file_gif: $!\n";
}

### Add the ZIP file
if (-f $my_file_zip) {
  $msg->attach (
    Type => 'application/pdf',
    Path => $my_file_zip,
    Filename => $your_file_zip,
    Disposition => 'attachment'
  ) or die "Error adding $file_pdf: $!\n";
}
Commented:
Hi, Waffe, seems you've been working on this for while?  It's not a homework is it?

If it's not, then you can change part of your code to:

### Add the GIF file
if(-f $file_gif)
{
$msg->attach (
  Type => 'audio/mp3',
  Path => $my_file_gif,
  Filename => $your_file_gif,
  Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";
}

### Add the ZIP file
if(-f $my_file_zip)
{
$msg->attach (
  Type => 'application/pdf',
  Path => $my_file_zip,
  Filename => $your_file_zip,
  Disposition => 'attachment'
) or die "Error adding $file_pdf: $!\n";
}

and what's with the open(IN, IN2... and flock(MBOX)?  I wonder if these are causing problems for you.  I did not even see in your code that you opened filehandle MBOX.  And I remember using MIME::lite you don't need to open attachement before sending them.  I think $msg->send || die "can't send mail--$!"; is all you need (and maybe the unlink statements).

Author

Commented:
No, this is not homework. Even though all I've been doing is learning.

Call me blind, but are both of your statements the same code. The only difference I can find is in inq123's 1st line, the "$" is named differently.

About the open (IN, IN2... and flock(MBOX). That's old code that is now obsolete. The only part I still need is the unlink. Didn't think it was causing problems but better to be safe, so I will remove it.

Little extra if no one minds, is there a better way to open my files and then close them then this:

open(IN, "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$mp3");
open(IN2, "/home/www/your.com/company/data_base/booking_tmp/$dynamicid$pdf");
close(IN);
close(IN2);

It seems like it could be done with just two lines instead of four, but I do not know the syntax.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:
When I tried to comment on your question, I guess davorg was already working on a comment.  When I submitted my comment, I saw his answer there, the same code as mine (except that mine has a typo and you found it out).  Things like this happen often as eager people like us always want to get some more points. :-)
Dave CrossPerl programmer, author and trainer

Commented:
> Little extra if no one minds, is there a better way to open my files and then close them then this:

You should check the return code from "open" and take appropriate action if it returns false.

Other than that, there's no better way to do it.

Author

Commented:
As I see it then davorg wins because he did beat inq123 to the punch but I will add a little and give it to ya inq123. I like keeping as many good, informed well-spoken people around.)

By the way, what do you people get for all the points you collect? When I first found this site I was like DiZam! Someone has finally done it, gave an incentive to people to answer questions. But now that I think about it I don't think there is any money going to you so, what is it?

Commented:
While I agree with davorg that you can't find better ways of doing open and close above.  But in your particular code, you could actually simply delete the two close lines and they'll be unlined later anyway.  It's a bad style to unlink open file handles for sure, but it'll work.  Consult unix documentation if you're surprised, provided you've not seen it before.  I remember I was when I first found it out.

BTW, another better way that I thought of earlier is that you should reuse the two file name variables you made earlier, and not type/paste those long file paths again. :-)

Should I get some points for that?  LOL

Commented:
Hey, waffe, the last two questions you always gave me a tiny chunk although I felt my solution was at least as good as others'.  Well, but it's your points to give away anyway and in this case davorg did get ahead in submitting first post.

Commented:
Well, I guess waffe you beat me too as I didn't even see your post above 'cause I was writing my comment.  Anyway you're right, nobody gets any money, and anybody can be an expert.  Some experts are more experienced and capable, but some others are not so good even though their points might be reasonably high (but people with quite high points are usually good, and early members in this community).

I guess for me the incentive is the bragging right, although my accomplishment so far is hardly something to brag about compared to some other experts.  But it still feels good. :-)

BTW, thanks for the points anyway!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial