How do I send email to my Gmail account using SMTP and Perl? - perl

I don't want to use sendmail to send an email but would prefer to use SMTP. How can I use Perl to send an email to my GMAIL account?

personally I would suggest you to use my module Email::Send::SMTP::TLS
which works pretty well through the TLS of Google Mail.
Thanks.
use Email::Send;
my $mailer = Email::Send->new( {
mailer => 'SMTP::TLS',
mailer_args => [
Host => 'smtp.gmail.com',
Port => 587,
User => 'username#gmail.com',
Password => 'password',
Hello => 'fayland.org',
]
} );
use Email::Simple::Creator; # or other Email::
my $email = Email::Simple->create(
header => [
From => 'username#gmail.com',
To => 'to#mail.com',
Subject => 'Subject title',
],
body => 'Content.',
);
eval { $mailer->send($email) };
die "Error sending email: $#" if $#;

As per the comment, it's not clear if you want to send email via Google's SMTP, or just send email in general (perhaps to your gmail account). You should check out Email::Send and possibly Email::Send::Gmail.
Alternatively, if what you're really asking is how do I move email from somewhere that isn't Gmail to Gmail, I've had very good luck with IMAP using Mail::Box and the Mail::Box::IMAP4::SSL backend. You can see an example of use here.

I've always used and had very good luck with Mail::Sender.

Another possibility you might want to look at is using the Email::Send::Gmail module from CPAN. This will allow you to send email from your Gmail account to any account (for example, to yourself)

There are muliple SMTP modules on CPAN, for example Net::ESMTP. Also, sendmail very probably does use SMTP to communicate with mail servers, so what's your real reason for not wanting to use it?

Email::Send (as used in Fayland Lam's answer) is deprecated:
Email::Send is going away... well, not really going away, but it's
being officially marked "out of favor."
This works for me, using the preferred Email::Sender:
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTPS ();
use Email::Simple ();
use Email::Simple::Creator ();
my $smtpserver = 'server';
my $smtpport = 587;
my $smtpuser = 'username';
my $smtppassword = 'password';
my $transport = Email::Sender::Transport::SMTPS->new({
host => $smtpserver,
port => $smtpport,
ssl => "starttls",
sasl_username => $smtpuser,
sasl_password => $smtppassword,
});
my $email = Email::Simple->create(
header => [
To => 'mymail#gmail.com',
From => 'sender#example.com',
Subject => 'Hi!',
],
body => "This is my message\n",
);
sendmail($email, { transport => $transport });

If you aren't familiar with CPAN (Comprehensive Perl Archive Network) I recommend you to bookmark that site. It contains third party (mostly well tested) libraries.
An example showing how to send emails using perl: http://www.perlfect.com/articles/sendmail.shtml

I happen to use MIME::Lite, which is a wrapper around Net::SMTP to simplify the process of building email objects, file attachments, and sending the payload.
If you're not familiar with installing modules, check:
On Windows, use the ActiveState Perl Package Manager (in start menu)
On Unix, use CPAN: $ sudo cpan Module::Name
On hosted Unix accounts: How can I install a CPAN module into a local directory?

If you just don't like sendmail, another option is to use Postfix, another MTA.
Here are the instructions I followed to get it setup on my machine, using gmail:
http://souptonuts.sourceforge.net/postfix_tutorial.html
This might be useful too, if you get a warning about failing to verify a certificate from Thawte Premium Server CA.
http://ubuntuforums.org/archive/index.php/t-894355.html

Related

Net::XMPP Perl module: Can this be used to check for chat messages on server?

I can now connect to my jabber server using the perl module Net::XMPP. Now I would like to know: Is it possible to monitor the server for incoming chat messages (NOT email messages) and act accordingly if one is recieved?
The basic code I currently have is:
#!/bin/perl -w
use strict;
use warnings;
use Net::XMPP;
my $con = new Net::XMPP::Client();
my $status = $con->Connect(
hostname => 'hostnamepart',
connectiontype => 'tcpip',
tls => 1,
ssl_ca_path =>'path for cert');
die('ERROR: XMPP connection failed') if ! defined($status);
The $status variable returns 1 when I run the code above which I assume means that I connected ok. However, now I am stuck! I have looked through the online documentation on the metacpan.org website for Net::XMPP but cannot figure out if it's even possible to do what I want. Any help appreciated.

Cake Email Times Out During Send

I'm trying to use CakeEmail in a web application, but I keep running into a timeout error. All my Googling and Stacking only give me the idea that something is not configured correctly, but I can't seem to find what config option I'm missing or filling incorrectly. I'm trying to send using my Gmail account.
Gmail Config:
public $gmail = array(
'host' => 'ssl://66.249.93.111',
'port' => 465,
'timeout' => 30,
'username' => 'my_gmail_account_name',
'password' => 'my_gmail_account_password',
'transport' => 'Smtp'
);
in app/Config/email.php
Email code:
$Email = new CakeEmail('gmail');
$Email->from(array('my_gmail_account_name' => 'Dev'));
$Email->to('my_gmail_account_name');
$Email->subject('Export Email Test');
$Email->send('This is a test email for ExportJobs.');
(As an additional note, the code that runs here is as part of a Cake Console program, so these methods are called when I run Console/cake file_name from the command line; also, that IP is the Gmail SMTP IP. When I try using the name, I get some DNS issue).
Does anyone happen to see what I'm missing?
Thanks for your time!
I found the problem I was having; it's a pretty silly error.
I completely forgot that to use the gmail domain for SMTP, I have to preface the domain name as "smtp.gmail.com". Once I did that it used SMTP and worked just fine.

I do i setup sendmail in php.ini on wampserver 2.1 to post form data on local machine ?

I have created some forms using zendframework on my local machine that send the form content via email.
I would like to test the functionality locally and have read some posts regarding configuring the php.ini file to do this but not sure which is the correct method ?
can anyone help me with this, many thanks
On Windows you will have to use SMTP to send the message. There is a drop in fake sendmail for Windows but it still requires an SMTP server.
You could use your ISP's sendmail server if they offer one, or you can set one up on the local machine. 1, 2, 3, 4
Since you are using Zend Framework, you can alternatively use Zend_Mail to send through an SMTP server (Zend_Mail can also use sendmail, but since it isn't configured, you can't use that transport). In that case see Sending via SMTP, SMTP Authentication, and Securing SMTP Transport.
Here is some sample code for sending an SMTP message with AUTH and TLS security.
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
$config = array('ssl' => 'tls',
'port' => '465', // 25 if no ssl
'auth' => 'login',
'username' => 'user',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.example.com', $config);
$mail = new Zend_Mail();
$mail->addTo('user#domain')
->setSubject('Mail Test')
->setBodyText("Hello,\nThis is a Zend Mail message...\n")
->setFrom('sender#domain');
try {
$mail->send($transport);
echo "Message sent!<br />\n";
} catch (Exception $ex) {
echo "Failed to send mail! " . $ex->getMessage() . "<br />\n";
}
Also note, your ISP may not require you to auth at all if you are sending from one of their IP addresses, but you probably do have to authenticate with your username and password, in which case you will want to use TLS.

SMTP Authentication problem

I'm writing a simple mail client in Perl, that uses SMTP to log in a Mail server, and from there sends a mail to another E-mail address (on different host). I use raw SMTP commands, because strawberry perl doesn't come with SASL.pm which is needed in order to authenticate. However, when the script tries to authenticate itself, it fails. I tried 'AUTH LOGIN' and 'AUTH PLAIN' mechanism but no luck. Here is an example:
EHLO example.com
AUTH LOGIN
YWxwaGE=
cGFzc3dvcmQ=
MAIL FROM:<alpha#example.com>
RCPT TO:<beta#betadomain.com>
DATA
Subject: sample message
From: alpha#example.com
To: beta#betadomain.com
Greetings,
Typed message (content)
Goodbye.
.
QUIT
EOT
"YWxwaGE=", "cGFzc3dvcmQ=" are the user and password encoded in Base64. Whatever I submit the server always complains that either the username or the password is wrong. What do I do wrong?
Why not just install a module that supports SMTP with SASL? One of the advantages of Strawberry Perl is that you can easily install modules from CPAN.
For example, Email::Simple and Email::Sender:
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Simple::Creator;
use Email::Sender::Transport::SMTP;
my $email = Email::Simple->create(
header => [
To => 'beta#betadomain.com',
From => 'alpha#example.com',
Subject => "sample message",
],
body => "Greetings,\nTyped message (content)\nGoodbye.\n",
);
my $transport = Email::Sender::Transport::SMTP->new({
host => 'smtp.example.com',
sasl_username => 'foo',
sasl_password => 'bar',
});
sendmail($email, { transport => $transport });

How can I send mail to Gmail with an attachment, in Perl?

How can I send mail to Gmail using Perl? Here's what I'm trying:
my $mailer = Email::Send->new(
{
mailer => 'SMTP::TLS',
mailer_args => [
Host => 'smtp.gmail.com',
Port => 587,
User => 'xxx',
Password => 'xxx',
]
}
);
use Email::Simple::Creator; # or other Email::
use File::Slurp;
#arrIrc = read_file("$ircFile");
my $email = Email::Simple->create(
header => [
From => 'xxx',
To => "$configList{email}",
Subject => "The summary of logfile $channelName",
],
body => "#arrIrc",
);
Use Net::SMTP::SSL to talk to GMail.
See MIME::Lite inline images on Perlmonks for an example.
If you want to send mail to Gmail, you do the same thing you would do to send mail anywhere. If you want to send mail through Gmail, there is the Email::Send::Gmail module. Merely typing your question in Google led me to Sending Mail Through Gmail with Perl by Mark Sanborn.
You can use MIME::Lite to compose a message, which you then send to your local sendmail process. However, in order to talk to gmail's servers you need to have SSL certificates set up. There's probably more detailed instructions for that on superuser.