Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How can I prevent my email from looking like spam?

by scorpio17 (Canon)
on Jan 03, 2008 at 15:39 UTC ( [id://660218]=perlquestion: print w/replies, xml ) Need Help??

scorpio17 has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a registration page using perl/cgi. Part of the registration process involves sending the user an email with a confirmation code. In my tests, this email frequently gets dumped into the spam folder! Here's a snippet to illustrate what I'm doing to send the email:

use strict; my $sendmail = "/usr/sbin/sendmail -t"; my $from = 'me@mycompany.com'; my $to = 'you@yourcompany.com'; my $reply_to = 'me@mycompany.com'; my $subject = "Subject goes here"; my $body = "Body goes here."; open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL "From: $from\n"; print SENDMAIL "To: $to\n"; print SENDMAIL "Reply-to: $reply_to\n"; print SENDMAIL "Subject: $subject\n"; print SENDMAIL "Content-type: text/plain\n\n"; print SENDMAIL $body; close(SENDMAIL);

I apologize if this seems off-topic, but perl is used a lot for this type of thing, and I'm hoping someone here knows a tip or trick (maybe some obscure field to use in the header?) that would make a short confirmation email look more legitimate, and less like spam.

Thanks!

Replies are listed 'Best First'.
Re: How can I prevent my email from looking like spam?
by gamache (Friar) on Jan 03, 2008 at 15:50 UTC
    If you are having trouble with your emails being detected as spam, try running one or two of them through something like SpamAssassin, and seeing what tests detect spam-nature. (The tests are usually named pretty sanely, but go here if you get stuck.) Once you know what to avoid, you'll have better luck avoiding it.

    For what it's worth, I don't think there's anything about the content of your message that makes it spammy, except for perhaps the shortness of the body. That might mean the tests you're failing are connected with your IP, hostname, domain, etc. rather than with your message itself.

Re: How can I prevent my email from looking like spam?
by moritz (Cardinal) on Jan 03, 2008 at 15:52 UTC
    I guess a big part of the problem is not perl-related.

    First you have to setup your mail server correctly, for example ensure that it works properly if the receiving server uses gray listing, sender verification callout and the like.

    And then make sure that your $subject and $body don't look like spam.

    Why do the spam filter classify your mail as spam? because of the headers? Or because of bayes classification of the text?

      I don't know why. The "To:" field was set to a gmail address for testing. I don't know what algorithm gmail uses to filter spam. I can send an identical email (in terms of subject, body, etc.) from another gmail account, and it goes through okay, some I'm guessing it's something in the header.
        Can you send messages directly from the same machine (i.e., not using the gmail website or any other webmail host) and have it go through OK?

        Pretty much the only time I see anything sent by my Perl flagged as spam is when I'm testing the code on my home workstation, which connects to the internet on an IP address from my ISP's dynamic address pool. Many mail servers are set up to notice that the mail is coming directly from a dynamic IP address rather than going through a "reputable" server and are more inclined to consider it spam as a result. Sending from a static IP address with reverse DNS set up correctly (or using your ISP's outgoing mail server as a relay/"smarthost" if you're not in a position to set up such a server yourself) tends to make that problem go away.

Re: How can I prevent my email from looking like spam?
by kirillm (Friar) on Jan 03, 2008 at 15:45 UTC

    Hi,

    MIME::Lite seems to be just what you're looking for.

    use MIME::Lite; my $msg = MIME::Lite->new( From => $from, To => $to, 'Reply-To' => $reply_to, Subject => $subject, Data => $body, ); $msg->send('sendmail', Sendmail => '/usr/sbin/sendmail');

    Update: I was asked how using MIME::Lite could help the OP. Well, it will add some additional important headers to the message, like MIME-Version, Content-Disposition, Content-Transfer-Encoding and Date. These will make the message look less like spam. The message in the OP did not have these headers. Some might be added by the MTA. In any case, it's vital to see why a system thinks the message is spam. Maybe the relay is in RBL or whatever.

Re: How can I prevent my email from looking like spam?
by grinder (Bishop) on Jan 03, 2008 at 20:47 UTC
    (maybe some obscure field to use in the header?)

    You're on the right track with this one. Older spam software was notorious (?) for getting by with the bare minimum number of headers. Spam detection software used this, and scored messages on the absence of such headers.

    I see you're injecting into the sendmail binary. I don't know how it behaves, as opposed to Postfix which will obligingly add missing Date and Message-ID headers. Be that as it may, your outgoing message should have both of those. If not, you'll have to add your own in your Perl program. Have a look at RFC-2822 for how to make sane values.

    Some other standard headers that you could add are :

    MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit

    (and make sure it really is 7 bit clean). Also adding Precedence: bulk or list might be a reasonably honest thing to do.

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://660218]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found