Find the answer to your Linux question:
Results 1 to 3 of 3
I have several sendmail scripts using perl and all work without problems. Now i am wanting to send an email with an attachment. For now for the purpose of testing ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    <SOLVED> Sendmail MIME::Lite and attachments


    I have several sendmail scripts using perl and all work without problems. Now i am wanting to send an email with an attachment.

    For now for the purpose of testing etc a simple text file but eventually it mat be either a Word Document or a pdf file.

    I'm struggling, i've tried various ways and failed.

    Can somebody help me.

    If possible i don't want to change the way i write my code to send mail, but if i have to in the end i will.

    A visitor fills out a webform that passes on entered info into a perl script. The script punches out a webpage to the visitor that includes the info they entered in the form. This works a treat. The script allso sends a number of emails, the info entered in the webform is included in the emails and works a treat.

    I want to attach an attachment to one of those emails and i'm failing.

    My code is:

    Code:
    open (MYMAIL, "|/usr/sbin/sendmail -t")
    || Error ('open', 'mail program');
    
    	print MYMAIL "MIME-Version: 1.0\n";
    	print MYMAIL "Content-Type: multipart/mixed\n";
    
    	print MYMAIL "To: \"$my_name\" <$googlemail_account>\; \"$my_name\" <$demon_account>\n";
    	print MYMAIL "From: \"$NameOfPerson - $BusinessName\" <$Email>\n";
    	print MYMAIL "Subject: Application Form WT$ReferralCode\n\n";
    
    	print MYMAIL "Content-Type: text/html\n";
    
    print MYMAIL "
    
    <p>Print HTML Code to body of Email</p>
    
    ";
    
    print MYMAIL "Content-Type: application/msword\n";
    print MYMAIL "Content-Transfer-Encoding: base64\n";
    print MYMAIL "Content-Disposition: attachment\n";
    
    use MIME::Lite;
    
    attach (
    
    path => "/srv/www/htdocs3/tempfileZ/testfile.txt",
    );
    
    
    close (MYMAIL);
    With the code as it is text printed in the body of the email includes all the html tags etc, this is something i want resolving too.

    use MIME::Lite and attach is not working.

    Thanks in advance

    Mark

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    Perl Code Sendmail Two different ways!

    This first Perl code below works and gives me what i want except i can't attach an attachment. It sends an email to a specific person at an email address and displays who it is from too. The email is being sent to an email address where there may be several people so the email gets to the person its intended for. The email gets sent from the person i want it to be sent from, there are three of us! It allso prints HTML text to the body of the email that includes info entered by the visitor on a webform.

    Code:
    open (MYMAIL, "|/usr/sbin/sendmail -t")
    || Error ('open', 'mail program');
    
    	print MYMAIL "MIME-Version: 1.0\n";
    	print MYMAIL "Content-Type: text/html\n";
    
    	print MYMAIL "From: \"$My_Name\" <$My_Email>\n";
    	print MYMAIL "To: \"$NameOfPerson - $BusinessName\" <$Email>\n";
    	print MYMAIL "Subject: TELECOMS-MESSAGE: Referrals Application Form WT$ReferralCode\n\n";
    
    print MYMAIL "
    
    <center><font color="red" size="8">Print HTML Message Here!</font></center>
    <br>
    <br>
    <br>
    <center>$NameOfPerson - $BusinessName</center>
    
    
    
    close (MYMAIL);
    This second Perl Code below sends an email to an email address (Not to a specific person at that address) and it is sent from an email address and (Not from a specific person). It does however send an email with an attachment and it does print HTML code to the body of the email including info entered on the original webform.

    Code:
    my (
    $msg,
    $message_body,
    $Subject
    );
    
    my $message_body = "
    
    <center><font color="red" size="8">Print HTML Message Here!</font></center>
    <br>
    <br>
    <br>
    <center>$NameOfPerson - $BusinessName</center>
    
    ";
    
    my $Subject = "TELECOMS-MESSAGE: Referrals Application Form WT$ReferralCode";
    
    use MIME::Lite;
    
    $msg = MIME::Lite->new (
    From => $Email,
    To => $demon_account,
    Subject => $Subject,
    Type => 'multipart/mixed'
    ) or die "Error Creating Multipart Container: $!\n";
    
    $msg->attach (
    Type => 'text/html',
    Data => $message_body
    ) or die "Error adding html text to body of email: $!\n";
    
    $msg->attach (
    Type => 'application/msword',
    Path => '/srv/www/htdocs3/tempfileZ/testfile.txt',
    Filename => 'testfile.txt',
    Disposition => 'attachment'
    ) or die "Error adding Attachment: $!\n";
    
    $msg->send;
    The two sets of code produces and sends two different emails, these are recieved and can be seen differently here:



    Neither give me what i want!

    I want both, i want to send an email To a specific person at an email address from a specific person at an email address and attach a file to the email and print HTML code to the body of the email containing info that the visitor entered on the initial webform.

    So basically how do i get peoples names $NameOfPerson and $My_Name into the To and From fields using MIME::Lite?

    Is This Possible?

    Please help, this is driving me nuts!

    Mark

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    3

    Solved

    The Following works

    Code:
    my (
    $msg,
    $message_body,
    $Subject
    );
    
    my $message_body = "
    
    <center><font color="red" size="8">Print HTML Message Here!</font></center>
    <br>
    <br>
    <br>
    <center>$NameOfPerson - $BusinessName</center>
    
    ";
    
    my $Subject = "TELECOMS-MESSAGE: Referrals Application Form WT$ReferralCode";
    
    use MIME::Lite;
    
    $msg = MIME::Lite->new (
    From => "$NameOfPerson - $BusinessName <$Email>",
    To => "$My_Name <$demon_account>; $My_Name <$googlemail_account>",
    Subject => $Subject,
    Type => 'multipart/mixed'
    ) or die "Error Creating Multipart Container: $!\n";
    
    $msg->attach (
    Type => 'text/html',
    Data => $message_body
    ) or die "Error adding html text to body of email: $!\n";
    
    $msg->attach (
    Type => 'application/msword',
    Path => '/srv/www/htdocs3/tempfileZ/testfile.txt',
    Filename => 'testfile.txt',
    Disposition => 'attachment'
    ) or die "Error adding Attachment: $!\n";
    
    $msg->send;
    Takes user input from webform displays to screen and punches out two emails both with the attachment and prints HTML text to body of email including user input from the webform.

    Done and Sorted

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •