Perl Net::SMTP - How to use - perl

Okay, I just spent the last four hours trying to figure out why my implementation of Net::SMTP module was no longer working.
In short, I was doing this (removing error checking stuff for clarity):
my $smtp = Net::SMTP->new(Host => $host);
$smtp->send($from);
$smtp->data;
$smtp->datasend($message);
$smtp->dataend;
$smtp->quit;
This had worked before, but now I was getting:
Net::SMTP=GLOB(0x7f9893114798)>>> SEND FROM:<from#address.com>
Net::SMTP=GLOB(0x7f9893114798)<<< 450 Unable to send to "<from#address.com" at this time
This was from my send method.
The Perldoc wasn't too much of a help because the send, send_and_mail, send_or_mail, and mail commands are all described together, and I had assumed were pretty much aliases of each other.
I looked at other examples and decided to try $smtp->mail and not $smtp->send That worked.
So, what are the differences between the send, mail, send_and_mail, and send_or_mail methods?. Do I need to try each one? I know they correspond to the MAIL, SEND, SOML and SAML commands in SMTP, but I can't really find anything that tells me what the differences are. And, do different SMTP servers use different commands. (Maybe that's why the send method previously worked?)

The Net::SMTP docs mention that you're supposed to be familiar with the SMTP protocol. That's currently specified by RFC5321. Appendix F.6 of RFC5321 discusses the now-obsolete SEND command. (It was supposed to deliver a message directly to the terminal, but that was rarely implemented. Some servers may have interpreted it as a synonym for MAIL.)
The short version is that you should be using the MAIL command. Or, even better, a higher-level module like Email::Sender.

From RFC 5321, appendix F.6:
"In addition to specifying a mechanism for delivering messages to
user's mailboxes, RFC 821 provided additional, optional, commands to
deliver messages directly to the user's terminal screen. These
commands (SEND, SAML, SOML) were rarely implemented, and changes in
workstation technology and the introduction of other protocols may
have rendered them obsolete even where they are implemented.
"Clients SHOULD NOT provide SEND, SAML, or SOML as services. Servers
MAY implement them. If they are implemented by servers, the
implementation model specified in RFC 821 MUST be used and the
command names MUST be published in the response to the EHLO command."

Related

Detect non-existing e-mail address without sending a message

I've read everything I could find on verifying e-mail addresses. The widely encountered solution is this, and it doesn't work (for one, actual nslookup output differs significantly from what the article shows, so I don't get an actual address to telnet to).
But then I thought: I don't need to verify the address. I just want to detect clearly bogus address (such an address that sending a message to it will yield "delivery failed" response). Is it possible to do in principle, and implement using C++ sockets or Java networking API in particular?
Depending on which operating system and tools you use, verifying the recipient's domain, and whether it is recorded in the DNS with a meaningful MX (mail exchange), you could use dig in place of nslookup. For foo#bar.com,
$ dig bar.com MX
Possibilities of detecting bogus eMail adresses are typically limited, though. Availability largely depends on how "generously" the MTA offers this information. Most don't, these days. The SMTP protocol includes some verbs you could then use, such as VRFY. On the other hand, spammers could do just that, hence … (That's one reason why a mail loop is run, in order to detect valid eMails fairly reliably; embedding, as I'm sure you know, a verification string to be sent back, or passed via URL to some web service.
SMTP, being a text protocol, would be used via some "transport layers" underlying higher level APIs like JavaMail. I'd look for programmability of these with the programming language used. Typically, there is some socket library, for sending and retrieving lines of text.

How can I read the importance of mails using Perl's Mail::IMAPClient?

I am using Mail::IMAPClient to connect to an IMAP server. Can someone advise on how to get the priority of the messages? I tried dumping the headers, but I cannot see where the importance is set.
That may depend on the client the sender used.
In my eyes, the most important importance setting is the header
Precedence: bulk
which indicates messages like delivery errors for which the sender doesn't even expect a failure notice, the mail system is simply free to drop these silently if any problems occur. (A good idea to set for outbound error messages, to avoid loops.)
Then, there is a header field called Importance which I'm not sure any mail client uses; RfC 4021 defines Priority, but it doesn't yet seem to be that common in the wild (hey, it's only been out for eight years!) and then there is the non-standard X-Priority, which may be what you are looking for.
Note that all of these are not part of the IMAP layer, but the message headers themselves. Depending on the IMAP server, IMAP flags may or may not have been set based on them, but to be sure you see what the sender thought about the mail (or their own ego), you may need to get the actual mail headers and look there.
That being said, most people I know simply ignore these values/appraisals, anyway.

procmail and delivering to an IMAP server?

I run my own mail server. It uses procmail to filter incoming mail, which is then stored in maildirs and gets served out my MUA using IMAP. I've got about 1.5GB of email is 135000 inodes.
This all works very nicely. However, I'd rather like to stop using maildir and switch to something more efficient --- maildb, or Dovecot's dbox, for example. Unfortunately, procmail can only deliver to a very limited set of backing store formats (Maildir, MH and mbox, AFAICT).
What I'd really like to do is to persuade procmail to deliver email via IMAP, rather than writing it directly to the backing store; this means that I can change the backing store format whenever I like without needing to reconfigure procmail. But I can't find any way of doing this. Any ideas?
(I'm also interested in any other mail filter tools that work like procmail but support IMAP. The only other filter tool I know is maildrop --- but that has similar restrictions to procmail.)
Okay, here's a proper solution.
The cone project (http://www.courier-mta.org/cone/cone00index.html, Debian project: cone) has a very handy tool called mailtool which will copy files between mailbox types, including remote IMAP servers.
So, to deliver a message to a remote mailbox, you need a script which:
writes out the incoming message to a file (which becomes a one-message mbox folder)
does mailtool -tofolder destinationfolder -copyto imaps://username:password#server.com mbox:/full/path/to/message.mbox
That will then upload the message.
I don't actually need to do this any more so don't have a prepared script to post, but of the eight or nine different IMAP tools available, this was the only one that would actually do this, so it's worth documenting as such.
As a partial answer to my own question, it seems that Dovecot does come with a deliver tool specifically designed for this kind of thing; it works from procmail with a line like:
| /usr/lib/dovecot/deliver -m "Folder.Name"
...and it figures out all the rest of the settings automatically.
So now I can change the Dovecot mail storage format and everything will still work; but I'd still like an approach that actually uses IMAP to deliver the messages, so that I can try IMAP servers other than Dovecot's.

Mail relays or SMTP services for use in code

I'm looking to start using an SMTP or mail relay service. I've found quite a few out there, but I'm not sure if there are advantages to one vs another. The only requirements I have are:
can send "from" more than 1 domain (possibly >20 for all the different sites I work on)
can pay for a higher limit (I may need to send as many as 15000 in 1 day, although the average is <500)
can send from PHP (although I doubt this will be a problem as most are compatible with any language)
I'm okay with an SMTP service, mail relay service or a site that uses a custom API, although an API would make the conversion more difficult.
Reasons for wanting to do this:
I don't want to host any mail services my self as they just cause head aches
I don't have to worry about being blacklisted. If they are blacklisted they will know about it and have the knowledge to get it fixed.
Reporting on if emails have gone through would be nice.
I'm not sure why you would need this. If you read the proper RFCs (822, 2822, 823, 2823), you should be able to connect to any given site directly using SMTP. You need to be a little careful with Line Endings (should always be CRLF), and should probably add mail.add_x_header = OFF to your php.ini.
However, if you need a relay, I recommend using a spam filtering provider, as then you have protection from being blacklisted due to spammers abusing email-generating forms. I would recommend Red Condor for this task, but that is only because I work there, and know that we can handle it.
I've started using Mandrill and found it to be a great, reliable service provided by MailChimp that includes enough for most sites to use for free. Easy to setup, but also has a lot more functionality available.

programmatically sending out emails from a linux server

I want to send out one off emails from a linux server. The server does not need to receive emails back.
Is there a simpler solution than sendmail for sending outgoing emails only?
I would prefer to use Perl to send the email.
Calling out to /usr/lib/sendmail is usually the preferred method because it handles delivery problems correctly. This does not mean using sendmail. Most (all?) mail transport system provide a command that's just named sendmail and provides an interface compatible to the original.
Alternatively you can send directly through a SMTP relay using a package like Net::SMTP but be extra sure to correctly cater for delivery problems.
Sendmail is not the only choice. you can use Postfix, Qmail, and many others
my Perl scripts call the mailx command. to know how to use it, type 'man mailx'
However this require to have a MTA correclty configured.
Or you can just use the Net::SMTP perl library and use your smtp server of choice
I have used msmtp successfully, ie it supports great authentication
TJ Luoma did a nice writeup of it on TUAW
I find nullmailer a very useful solution for the described scenario. Nullmailer is a sendmail/qmail/etc replacement MTA which relays to a fixed set of mail servers. It is very simple to configure and consumes little resources.
One important advantage of nullmailer over other mentioned solutions like mstmp and ssmtp is that it maintains a queue of emails to be sent. The application sending the mail is blocked only a very short time while the mail is queued (milliseconds). The sending of the mail happens in the context of a another process.
Solutions like mstmp and ssmtp don't maintain a queue of email. The sending happens in the context of the application, thereby blocking the application. Sending an email this way can easily take 1 to 2 seconds or longer. This may not be a problem in many cases but can become a problem if the email is sent by a web application.
http://untroubled.org/nullmailer/
You do not need a mail transport agent (MTA) instance on the machine doing the sending if you have another mailserver already running in your organization: you can make Perl deliver the email through SMTP to that server, so there's no need for having (another) MTA like sendmail on "your" machine.
I'm fond of creating a gmail or other free account and then using the java mail api in J2EE to send messages (from your new gmail account) to whoever... Typically i'll create a Mailer class which can be constructed with a default constructor and then give it a send(String dest, String subj, String body[, Obj attach...if you want]) and then in your case you might wrap the thing in a main method so that you can call it from else with some command line args, or call from within some java program. If you interested i'll shoot you the code.
I know you said perl, but the simplest cross-platform email sending library I have used is python's smtplib. Certainly worth a look.