waffe
asked on
Email attachments must be present or email will not send.
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/compan y/data_bas e/booking_ tmp/$dynam icid$mp3";
my $your_file_gif = 'your_file.mp3';
my $my_file_zip = "/home/www/your.com/compan y/data_bas e/booking_ tmp/$dynam icid$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/compan y/data_bas e/booking_ tmp/$dynam icid$mp3") ;
open(IN2, "/home/www/your.com/compan y/data_bas e/booking_ tmp/$dynam icid$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/d ata_base/b ooking_tmp /$dynamici d$mp3");
unlink("/home/www/your.com /company/d ata_base/b ooking_tmp /$dynamici d$pdf");
#!/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/compan
my $your_file_gif = 'your_file.mp3';
my $my_file_zip = "/home/www/your.com/compan
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/compan
open(IN2, "/home/www/your.com/compan
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
unlink("/home/www/your.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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. :-)
> 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.
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.
ASKER
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?
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?
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
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
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.
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!
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!
ASKER
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/compan
open(IN2, "/home/www/your.com/compan
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.