Gossamer Forum
Home : Products : Links 2.0 : Customization :

send newsletter per cronjob

Quote Reply
send newsletter per cronjob
Hi!
I was a little bit puzzled about all 10 days sending the "what's new"-mails manualy. Most the time, I forgot about it. So I did a little script to run per cron. It's not yet testet very well, and it's at all nothing special, just some drag'n'drop from different links-sources. Have a look at it, and send bug-reports and sugestions.

Code:
#!/usr/bin/perl

eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \

require 5.001; # Make sure we have at least perl 5.001.
require "links.cfg"; # Change this to full path to links.cfg if you have problems.
require "$db_lib_path/links.def"; # Database Definitions
require "$db_lib_path/db.pl"; # Database Routines.
require "$db_lib_path/db_utils.pl"; # Database Support utilities.
require "$db_lib_path/Mailer.pm"; # Mailing Routines
};

# edit this part for translation or better text
my $cfg_title = "What's new " . &get_date();
my $cfg_mail_footer = qq|--------------------------------------------------------------------
To unsubscribe from this mailing list, just visit:
$build_email_url?action=unsubscribe&email=<%Contact Email%>|;

# nothing to edit under this line

&main();

sub main{
my $message = &build_new_links . $cfg_mail_footer;
my $subscribers = &build_email_list;
my $subject = $cfg_title;

my $start = time();
print qq|Mailing started on |, scalar localtime($start), "\n";

# Make sure we have everything we need.
my %seen;
$message =~ s/\r//g;

$message or (print "No message defined! Aborting!" and exit);
$subject or (print "No subject defined! Aborting!" and exit);
$db_admin_email or
(print "An admin email has not been set in the config. Aborting!" and exit);
($db_smtp_server or $db_mail_path) or
(print "No SMTP server or Sendmail has been defiend in the config. Aborting!" and exit);

# Let's initilize a mailer.
my $mailer = new Mailer ( { smtp => $db_smtp_server, sendmail => $db_mail_path,
from => $db_admin_email, subject => $subject,
msg => $message,
log => $db_mailer_log } ) or &cgierr("Unable to init mailer! Reeason: $Mailer::error");

print qq~
Mailing Message:
--------------------------------------------------------
From: $db_admin_email
Subject: $subject

$message
--------------------------------------------------------
~;

# And begin mailing!
my ($email_q, $email_n, $href, $msg_mod);

open (DB, "<$db_email_name ") or &cgierr("unable to open db file: $db_email_name .\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
chomp;
($email, $name) = split /\Q$db_delim\E/;

$email_q = &urlencode ($email);

# Make sure the address looks ok, and we haven't already mailed to this email address.
($email =~ /.+@.+\..+/) or (print qq~Invalid email address: '$email'. Skipping.\n~ and next);
($seen{$email}++) and (print qq~Already mailed to: '$email'!\n~ and next);

$msg_mod = &load_template('email', { 'Contact Email' => $email }, $message);
$mailer->send ( { to => $email, msg => $msg_mod } ) ?
(print qq~Message sent succesfully to '$email'\n~) :
(print qq~Unable to mail to '$email'. Reason: $Mailer::error\n~);
} # end of while

# All done, print summary stats.
my $finish = time();
my $elapsed = $finish - $start;
print "--------------------------------------------------------\n";
print "Mailing finished at: ", scalar localtime($finish), ".\nElapsed: $elapsed s.\n";
close DB;

} # end of main
Bye
Tiggr

Quote Reply
Re: send newsletter per cronjob In reply to
Thanks for the contribution...

Regards,

Eliot Lee
Quote Reply
Re: send newsletter per cronjob In reply to
Tiggr,

Thanks from me too. You may want to add this to the Resource Center at http://www.gossamer-threads.com/...pts/resources/Links/ so that it doesn't get lost in all the messages here.

- Bobsie
bobsie@orphanage.com
http://goodstuff.orphanage.com/
Quote Reply
Re: send newsletter per cronjob In reply to
Hi!

> You may want to add this to the Resource Center

Yes, I will do this soon! But before I add it to the Resource Center, I will do some more testing if all works like expected!

Bye
Tiggr

Quote Reply
Re: send newsletter per cronjob In reply to
Hi,

Nice script by the way.

How would you modify it to include txt/html from a seperate file ( For ex. from data.txt )to include in all emails being sent by cron? (Instead of new links )

Thanks.

Quote Reply
Re: send newsletter per cronjob In reply to
Hi!
I have done a little homepage for this very simple script. U may have a look at it under http://wwwisulizer.hypermart.net/newsletter.html.

Don't care about the BAD english and the typos...

I will add this to the resource center too!

Bye
Tiggr



Quote Reply
Re: send newsletter per cronjob In reply to
I was just looking at the page you did and saw this:

Code:
0 4 10,20,30 * * <your path>links/admin/newsletter.pl
you might want to change that to:

Code:
0 4 8,18,28 * * <your path>links/admin/newsletter.pl
so that the third newsletter will be sent in February.

--Drew
Quote Reply
Re: send newsletter per cronjob In reply to
Hi!

In Reply To:
How would you modify it to include txt/html from a seperate file
This shouldn't be very hard!

Code:
my $message = &build_new_links . $cfg_mail_footer;
should be changed to something like:

Code:
my $message ;
open MAILTXT, "your file" or &cgierr(".Can't open \"your file\"!");
while <MAILTEXT> { $message .= $_; } # read file
close MAILTXT;
$message .= $cfg_mail_footer;
I've never tested this. Don't take it as solution, but as tip!

Thanks
Tiggr

Quote Reply
Re: send newsletter per cronjob In reply to
Hi!
In Reply To:
you might want to change that to:

Code:
0 4 8,18,28 * * <your path>links/admin/newsletter.pl
so that the third newsletter will be sent in February.
Very good point! Thank you very much!

Bye
Tiggr

Quote Reply
CRON? HOW TO SET IT UP? In reply to
Maybe it's me, but can anyone help me to understand how to set up a simple cron?
It seems stupid to you but not to me
Cristinba

Quote Reply
Re: CRON? HOW TO SET IT UP? In reply to
Hi!
Please have a look at this page:
http://www.bobsgoodstufflists.net/cron_instr.html

(I found in bye a quick search for "cron" in the resource center!) It has very good instruktions on how to use cron.

I hope this will help you!

Bye
Tiggr

Last edited by:

Bobsie: Feb 13, 2006, 2:52 PM
Quote Reply
Re: CRON? HOW TO SET IT UP? In reply to
Thanks...
When they say
"Login to your shell account and go to your Links admin directory"
Do they mean with TELNET???
Cristina

Quote Reply
Re: CRON? HOW TO SET IT UP? In reply to
YES!

Regards,

Eliot Lee