#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Posts
    679
    Rep Power
    12

    MIME::Lite send emails from a remote server


    Hello,

    I am using the following code to send emails using MIME::Lite

    Code:
    use MIME::Lite;
    use Net::SMTP;
    
    ### Adjust sender, recipient and your SMTP mailhost
    my $from_address = 'martin dot zahn at akadia dot ch';
    my $to_address = 'martin dot zahn at akadia dot ch';
    my $mail_host = 'mailhost.domain.com';
    
    ### Adjust subject and body message
    my $subject = 'A message with 2 parts ...';
    my $message_body = "Here's the attachment file(s) you wanted";
    
    ### Adjust the filenames
    my $my_file_gif = 'my_file.gif';
    my $your_file_gif = 'your_file.gif';
    my $my_file_zip = 'my_file.zip';
    my $your_file_zip = 'your_file.zip';
    
    ### Create the multipart container
    $msg = MIME::Lite->new (
      From => $from_address,
      To => $to_address,
      Subject => $subject,
      Type =>'multipart/mixed'
    ) or die "Error creating multipart container: $!\n";
    
    ### Add the text message part
    $msg->attach (
      Type => 'TEXT',
      Data => $message_body
    ) or die "Error adding the text message part: $!\n";
    
    ### Add the GIF file
    $msg->attach (
       Type => 'image/gif',
       Path => $my_file_gif,
       Filename => $your_file_gif,
       Disposition => 'attachment'
    ) or die "Error adding $file_gif: $!\n";
    
    ### Add the ZIP file
    $msg->attach (
       Type => 'application/zip',
       Path => $my_file_zip,
       Filename => $your_file_zip,
       Disposition => 'attachment'
    ) or die "Error adding $file_zip: $!\n";
    
    ### Send the Message
    MIME::Lite->send('smtp', $mail_host, Timeout=>60);
    $msg->send;
    It doesn't connect to the specified $mail_host, it connects to local SMTP server
    Code:
    220-local.smtp ESMTP Exim 4.80 #2 Sat, 09 Mar 2013 14:44:11 +0200
    MIME::Lite::SMTP=GLOB(0xd2d8a0)<<< 220-We do not authorize the use of this system to transport unsolicited,
    MIME::Lite::SMTP=GLOB(0xd2d8a0)<<< 220 and/or bulk e-mail.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Does your smtp server require authentication?

    Code:
    MIME::Lite->send('smtp', $mail_host, Timeout=>60,
                                         AuthUser=>$user,
                                         AuthPass=>$pass
    );
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Posts
    679
    Rep Power
    12
    Yes, but it connects to the local smtp server, not to the remote one.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    It will connect to the server you specify.

    Your send statement is specifying $mail_host (I'm assuming form your code that is a remote server). If that server requires authentication, then you'll need to pass that info in the send statement like I showed.

    If that server is on your local network, then the email should go through once you authenticate; assuming that the server is setup correctly. If that server is not local then it still may fail even after authenticating if they don't allow cross domain relay.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Posts
    679
    Rep Power
    12
    The mailserver is the remote server, but it requires to authenticate to local smtp server. Is it a filter required by EXIM?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Posts
    679
    Rep Power
    12
    I think this is the flow:
    mailx → /usr/bin/sendmail → local MTA → Gmail or ISP/work servers → recipient MTA → recipient inbox

    I need it to be:
    app → Gmail or ISP/work servers → recipient MTA → recipient inbox

IMN logo majestic logo threadwatch logo seochat tools logo