Send Bulk Email from .TXT File w/ Linux (Ubuntu 9.04)

  • Thread starter cepheid
  • Start date
  • Tags
    List
In summary: Net::SMTP->new( $mail_server );## $mail->from($from);# $mail->to($to);# $mail->message_ref($message_ref);# $mail->send;
  • #1
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,199
38
If I have a .TXT file with comma-separated e-mail addresses, is there some way that I can quickly send an e-mail out with all of them in the BCC field? For example, could this be done from the command line in Linux (Ubuntu 9.04)? Is there some way that I could have the contents of another .TXT file that is the e-mail message be automatically sent to everyone on the list in this manner?

My user account on my department computer is capable of sending outgoing mail through the department's mail server, so I don't think that should be a problem.
 
Computer science news on Phys.org
  • #2
Obviously you didn't simply copy the comma-delimited list to the clipboard and paste it into the Bcc field in your mail client, or you would have done so. So what's the catch?
 
  • #3
DaveC426913 said:
Obviously you didn't simply copy the comma-delimited list to the clipboard and paste it into the Bcc field in your mail client, or you would have done so. So what's the catch?

I was wondering if there was a faster way of doing it straight from the command line.

In the past I have done what you suggested, but it is complicated by the fact that I don't use a mail client on my desktop Linux machine in the department. The only computer I have on which I use a mail client is my Mac (a laptop). I default to webmail (Gmail) on my Linux machine. To elaborate: everything forwards to Gmail, which I can either access directly with a browser, or which I can read from my mail client on my Mac. Furthermore, the situation is complicated by the fact that a third computer is involved. The e-mail contact .TXT file in question is not maintained on either of the machines I just mentioned. It is located under a different user account on the department server. So, the steps involved are usually:

1. Use a terminal to access the department server and get a local copy of the list on one of the two computers I use (desktop Linux machine or laptop Mac).

2. Open up this local copy of the list, select all nearly 400 e-mail addresses, and paste into BCC field in either web broswer accessing Gmail or Mail client program on Mac.

3. Compose message and hit send.

If there was a way to send an e-mail out to the list directly from the command line, then I wouldn't have to worry about creating a local copy of that list. Also, it would be awesome if the message sent could also be the contents of (another) .TXT file. Then I wouldn't have to worry about using some sort of mail program in which the message had to be composed.
 
  • #4
There's no way to set up some sort of e-mail client so that you can set up user groups? That would be so simple.
 
  • #5
Last edited by a moderator:
  • #6
Does your Linux box have sendmail on it? If so, prepare a file that contains the necessary mail headers (including Bcc:), followed by an empty line and then the message body. Let's call it message.txt. Then do something like this:

% sendmail [options] < message.txt

You'll have to find out which options you need to specify. I think you can tell it to connect to a remote mail server instead of using the local machine as the mail server. It's been years since I did this (actually it was inside a Perl script) so I don't remember the details.

A common "sendmail replacement" is Postfix, which I think is usually installed so that you can invoke it under the name 'sendmail' with the same options. Mac OS X comes with Postfix installed, or at least it once did.

[added] Or, as rootX noted, if you know Perl, you can whip up a script that uses a Perl module to send mail. I've used the Net::SMTP module which I think comes with any standard Perl installation. Here's a subroutine that I once wrote for this:

Code:
#--------------------------------------------------------------------------
#  Sends an e-mail using the Net::SMTP module.
#
#  Receives:
#    $From = address which will appear in the "From:" line.
#    $To = address to send the message to
#    $MessageRef = reference to an array that contains the complete
#  message, both header and body.
#
#  Example:
#  SendMail ("me\@podunk.edu", "you\@somewhere.com",
#    \@MessageArray);
#
#  The global variable $MailServer must contain the domain name
#  of the mail server to be used (e.g. mail.podunk.edu).
#
#  Make sure you have the statement 'use Net::SMTP;' at the beginning of
#  your script.
#
#  If one of the SMTP calls returns an error code, the message is printed
#  to standard output, along with the message itself.
#
#  To get the above output always, set $DEBUG = 1 below; set it to 0 to
#  turn this off.
#--------------------------------------------------------------------------

sub SendMail {
    my ($From, $To, $MessageRef) = @_;
    my $DEBUG = 0;

    my $MailConnection = Net::SMTP->new($MailServer)
      or die "Can't connect to mail server $MailServer!\n";

    $MailConnection->mail($From)
      or die "Can't start message from $From\n";

    if ($MailConnection->to($To)) {
        $MailConnection->data();
        $MailConnection->datasend (@$MessageRef);
        $MailConnection->dataend();
    } else {
        print "Can't start message to $To!\n";
    }
    if ($MailConnection->ok()) {
        $DEBUG && print "\nResult from mail server:\n";
        $DEBUG && print $MailConnection->code();
        $DEBUG && print " ";
        $DEBUG && print $MailConnection->message();
        $DEBUG && print "\n";
        $DEBUG && print "---------- message -----------\n";
        $DEBUG && print @$MessageRef;
    } else {
        print "\nMail failed!  Response from mail server:\n";
        print $MailConnection->code();
        print " ";
        print $MailConnection->message();
        print "\n";
        print "---------- attempted message -----------\n";
        print @$MessageRef;
    }

    $MailConnection->quit();
}
 
Last edited:
  • #7
Wow, thanks for all the info guys! I will look through it and see if I can figure something out. I have general programming experience, but have never done any Perl scripting. That having been said, if I just figure out how to use sendmail, then I can manually run it from the command line with the requisite options each time, correct?

By the way, I typed man sendmail at the command prompt while SSH'd into my department's server (where the contact list is kept), and there was a man entry for it. So I guess that means I'm good to go, right?

EDIT: Evidently not. I get an error message saying "command not found" when I try to actually use it.
 
  • #8
cepheid said:
EDIT: Evidently not. I get an error message saying "command not found" when I try to actually use it.

WHOAH! Actually, it works fine! Googling just revealed that this application is located at /usr/sbin/sendmail, and that I just had to use the full path in the command. Also, the option you need in order for it to look for headers in the message text files is -t.
 
  • #9
I don't know much about perl but I think you need to point to the place where Net:SMTP/sendmail module is located. Personally, I just download sendmail file to the same folder and compiled without errors.

[Edit: 1 minute delayed]
 

FAQ: Send Bulk Email from .TXT File w/ Linux (Ubuntu 9.04)

1. How do I send bulk emails from a .TXT file using Linux (Ubuntu 9.04)?

To send bulk emails from a .TXT file using Linux (Ubuntu 9.04), you can use the command line tool "mail" along with a simple shell script. The script will loop through the email addresses in the .TXT file and send out individual emails to each recipient.

2. Can I customize the email content for each recipient when sending bulk emails from a .TXT file using Linux (Ubuntu 9.04)?

Yes, you can customize the email content for each recipient by using variables in your shell script. You can extract information from the .TXT file and use it to personalize the email for each recipient.

3. Is there a limit to the number of emails I can send using this method?

The limit for sending bulk emails from a .TXT file using Linux (Ubuntu 9.04) will depend on your email server and any restrictions set by your internet service provider. It is recommended to check with your provider to ensure you are not violating any terms of service.

4. Can I schedule the bulk email sending process to run at a specific time?

Yes, you can use the "cron" tool to schedule the shell script to run at a specific time. This will allow you to automate the bulk email sending process without any manual intervention.

5. How can I ensure that the emails are successfully sent and delivered to all recipients?

You can use the "-v" flag in the "mail" command to get a verbose output, which will provide information on whether the email was successfully sent or if there were any errors. You can also check your email server logs to track the delivery status of the emails.

Similar threads

Replies
10
Views
1K
Replies
13
Views
3K
Replies
2
Views
3K
Back
Top