M
M.O.B. i L.
I'm trying to write a program that can send out mails to companies where
I'm interested in working. These mails are the same except that the
company name is switched. My CV should be attached as a pdf-file.
The problem is that I don't get one attachment, but ten! Also the
message is repeated 10 times. If I change the program by uncommenting
the commented line in the function load() it works but then it doesn't
send the correct pdf-file. The CV is 129490 bytes. How could I change
this program so that there is only one attachment and no repeated messages?
In the program below some long lines might have been splitted.
#!/usr/bin/perl
# jobapplier.pl vN/A (Buggy!)
# Author: Mikael O. Bonnier, (e-mail address removed),
http://www.df.lth.se/~mikaelb/
# Copyright (C) 2008 by Mikael O. Bonnier, Lund, Sweden.
# License: GNU GPL v3 or later, http://www.gnu.org/licenses/gpl-3.0.txt
use strict;
use Email::Send;
use Email::Simple::Creator;
use Email::MIME::Modifier;
use DBI;
use MIME::Words qw/encode_mimewords/;
use File::stat;
my %settings = (
SMTP => 'mail.df.lth.se',
PORT => 588,
FROM_DOMAIN => 'cust.bredbandsbolaget.se',
DB => 'jobapplier_test',
DB_HOST => 'localhost',
FROM => '(e-mail address removed)',
SUBJECT => 'Intresseanmälan som fysiker eller programutvecklare',
BODY =>
'Det skulle vara intressant att jobba hos er på $cCompany.',
UPLDDIR => '../Documents/LyX',
FILE => 'MOB_CV.pdf',
);
@ARGV == 4
or die
'Provide username and password for the SMTP and the MySQL server,
respectively,'
. " on the command line.\n";
@settings{qw( USER PASS DB_USER DB_PASS)} = @ARGV;
my $dbh = DBI->connect( "DBI:mysql:$settings{DB}:$settings{DB_HOST}",
$settings{DB_USER}, $settings{DB_PASS} );
my $q = q|select cCompany, cEmail
from Contacts
order by cId asc|;
my $sth = $dbh->prepare($q);
$sth->execute;
my $as = [];
while ( my $hr = $sth->fetchrow_hashref ) {
push @{$as}, $hr;
}
$dbh->disconnect;
my $mailer = Email::Send->new(
{
mailer => 'SMTP::TLS',
mailer_args => [
Host => $settings{SMTP},
Port => $settings{PORT},
User => $settings{USER},
Password => $settings{PASS},
Hello => $settings{FROM_DOMAIN},
]
}
);
for my $row (@$as) {
my $body_text = $settings{BODY};
$body_text =~ s/\$cCompany/$row->{cCompany}/g;
my $email = Email::Simple->create(
header => [
From => $settings{FROM},
To => $row->{cEmail},
Subject => encode_mimewords( $settings{SUBJECT} ),
'Content-Type' => 'text/plain; charset="utf-8"',
],
body => $body_text,
);
my $body = load( $settings{UPLDDIR}, $settings{FILE} );
my $part = Email::MIME->new('');
$part->body_set($body);
$part->disposition_set('attachment');
$part->name_set( $settings{FILE} ); # Both needed?
$part->filename_set( $settings{FILE} ); # Both needed?
$part->encoding_set('base64');
$part->content_type_set('application/octet-stream');
my @more_parts = ($part);
$email = Email::MIME->new( $email->as_string );
$email->parts_add( \@more_parts );
eval { $mailer->send($email) };
die "Error sending email: $@" if $@;
}
sub load {
my ( $upldDir, $inFile ) = @_;
my $DNLDFILE;
open $DNLDFILE, '<', "$upldDir/$inFile";
binmode $DNLDFILE;
my $st = stat $DNLDFILE;
my $content;
read $DNLDFILE, $content, $st->size;
close $DNLDFILE;
# $content = 'ABC';
return $content;
}
__END__
I'm interested in working. These mails are the same except that the
company name is switched. My CV should be attached as a pdf-file.
The problem is that I don't get one attachment, but ten! Also the
message is repeated 10 times. If I change the program by uncommenting
the commented line in the function load() it works but then it doesn't
send the correct pdf-file. The CV is 129490 bytes. How could I change
this program so that there is only one attachment and no repeated messages?
In the program below some long lines might have been splitted.
#!/usr/bin/perl
# jobapplier.pl vN/A (Buggy!)
# Author: Mikael O. Bonnier, (e-mail address removed),
http://www.df.lth.se/~mikaelb/
# Copyright (C) 2008 by Mikael O. Bonnier, Lund, Sweden.
# License: GNU GPL v3 or later, http://www.gnu.org/licenses/gpl-3.0.txt
use strict;
use Email::Send;
use Email::Simple::Creator;
use Email::MIME::Modifier;
use DBI;
use MIME::Words qw/encode_mimewords/;
use File::stat;
my %settings = (
SMTP => 'mail.df.lth.se',
PORT => 588,
FROM_DOMAIN => 'cust.bredbandsbolaget.se',
DB => 'jobapplier_test',
DB_HOST => 'localhost',
FROM => '(e-mail address removed)',
SUBJECT => 'Intresseanmälan som fysiker eller programutvecklare',
BODY =>
'Det skulle vara intressant att jobba hos er på $cCompany.',
UPLDDIR => '../Documents/LyX',
FILE => 'MOB_CV.pdf',
);
@ARGV == 4
or die
'Provide username and password for the SMTP and the MySQL server,
respectively,'
. " on the command line.\n";
@settings{qw( USER PASS DB_USER DB_PASS)} = @ARGV;
my $dbh = DBI->connect( "DBI:mysql:$settings{DB}:$settings{DB_HOST}",
$settings{DB_USER}, $settings{DB_PASS} );
my $q = q|select cCompany, cEmail
from Contacts
order by cId asc|;
my $sth = $dbh->prepare($q);
$sth->execute;
my $as = [];
while ( my $hr = $sth->fetchrow_hashref ) {
push @{$as}, $hr;
}
$dbh->disconnect;
my $mailer = Email::Send->new(
{
mailer => 'SMTP::TLS',
mailer_args => [
Host => $settings{SMTP},
Port => $settings{PORT},
User => $settings{USER},
Password => $settings{PASS},
Hello => $settings{FROM_DOMAIN},
]
}
);
for my $row (@$as) {
my $body_text = $settings{BODY};
$body_text =~ s/\$cCompany/$row->{cCompany}/g;
my $email = Email::Simple->create(
header => [
From => $settings{FROM},
To => $row->{cEmail},
Subject => encode_mimewords( $settings{SUBJECT} ),
'Content-Type' => 'text/plain; charset="utf-8"',
],
body => $body_text,
);
my $body = load( $settings{UPLDDIR}, $settings{FILE} );
my $part = Email::MIME->new('');
$part->body_set($body);
$part->disposition_set('attachment');
$part->name_set( $settings{FILE} ); # Both needed?
$part->filename_set( $settings{FILE} ); # Both needed?
$part->encoding_set('base64');
$part->content_type_set('application/octet-stream');
my @more_parts = ($part);
$email = Email::MIME->new( $email->as_string );
$email->parts_add( \@more_parts );
eval { $mailer->send($email) };
die "Error sending email: $@" if $@;
}
sub load {
my ( $upldDir, $inFile ) = @_;
my $DNLDFILE;
open $DNLDFILE, '<', "$upldDir/$inFile";
binmode $DNLDFILE;
my $st = stat $DNLDFILE;
my $content;
read $DNLDFILE, $content, $st->size;
close $DNLDFILE;
# $content = 'ABC';
return $content;
}
__END__