Perl SMTP auth fails when password is passed via variable - perl

I have several hundred Perl scripts that send email via gmail SMTP. All are, and have been working fine for a couple of years with the gmail password hard-coded.
To simplify password changes (and for security), I want to move the password to an env var, and pull that into the perl scripts. I am using the code below.
my $smtp = Net::SMTP::SSL->new('smtp.gmail.com', Port => 465);
my $passwd = $ENV{'PASSWD'}; # I added this line
$smtp->auth('email#gmail.com', $passwd); # I replaced 'theHardCodedPassWord' with $passwd
The same method works in a non-smtp scenario -- I pull info from an env var and use it.
What am I doing wrong?

Two obvious debugging approaches:
Print the values of $ENV{PASSWD} and $passwd before the call to auth().
Turn on debug output in the Net::SMTP::SSL object (add Debug => 1 to the call to new()).
A few other points:
The documentation for Net::SMTP::SSL is pretty clear that the module is deprecated and should no longer be used. It says that Net::SMTP now has built-in support for SSL connections.
Net::SMTP seems to be a rather low-level approach to this problem. Have you considered Email::Send::Gmail? I've even had success sending email through GMail with Email::Sender::Simple.
I know that GMail recently hardened their security around using their SMTP servers from external apps. There's more information about this change on the Google support site.

Related

Is it possible to specify Exchange version when sending email with PowerShell?

I am trying to send an email via Exchange using PowerShell. Is it possible to specify what version of Exchange to use? I don't know if Send-MailMessage is the correct function to use either.
The server requires username and password, and should use Exchange2007_SP1.
Running the code below returns "Send-MailMessage: Service Not Available. Closing Channel. Server response 4.3.2 Service Not Available".
So it seems that the server gets this, but rejects it.
The secured server that this runs on, has no direct internet connection, but can reach the Exchange server. Hence, I cannot download and install addons to PowerShell, if that is required.
Any ideas out there? Am I attacking this from the wrong angle?
$sendMailParams = #{
From = 'sender#somemailaccount.com'
To = 'johndoe#someexchangeserver.com'
Subject = 'some subject here'
Body = 'some body here'
SMTPServer = 'exchange.server.com'
UseSsl = $true
Credential = $mailCred
}
Send-MailMessage #sendMailParams```
The reason for this not working in the first place is due to PowerShell policies on the terminal servers. For security reasons, sending mail from PowerShell has been disabled, since you can mail to anyone, from anyone. When you log on to the system in the morning, you get a random terminal server. If I'd been logged on to the same one all the time, it might have been an option to enable it on that specific terminal server. But as it is, it is too open to enable it on all servers, so that's why it is restricted.
If things change in the future, it might be an option to do this.
I know this isn't a direct answer to the question, but it is no longer relevant for me. IT says it isn't necessary to specify Exchange version when sending through PowerShell.
Thanks for all your inputs, folks. You are the best.

Jenkins won't send e-mails anymore

This might be a tough thing to figure out but i have to give it a try.
With no changes i am aware of, my jenkins just stopped sending out e-mail notifications. I use jenkins 1.609 and email extension plugin 2.40.5. Jenkins runs as a service using a service user who also has the rights on the server to send e-mails. Ports to our smtp-server are unlocked. Used telnet for testing that everything works as expected. SMTP-server works fine, so it has something to do with jenkins and i cant figure out what is going wrong.
For some unknown reason jenkins still fails to send e-mails. When i use the test-feature that jenkins offers i get: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful which tells me that either user or pw is incorrect, but as mentioned i've checked many times, it's setup correctly. Can anyone help out with this?
UPDATE:
After decrypting the password i can safely say that jenkins does save the password correct. Is there any way i can check that jenkins actually compares the correct encrypted passwords with each other? From my understanding jenkins uses 2 different encrypting techniques, maybe it uses tech1 for stored pw and tech2 for compare pw?
ANOTHER UPDATE:
Just tried adding a new certificate to the keystore, just in case, still no change.
MORE UPDATES:
Tried updating the mailer plugin- no change
Tried different users - all failed with the same error
So no matter what user i try, they all fail, but have jenkins execute a script which sends e-mails using said users it works.
August 18, 2016 - no change

I cannot send mail with SwiftMailer or PHPMailer from Localhost using gmail

I have recently been unable to send out mails through php on localhost. I use PHPMailer and SwiftMailer in different parts of the application.
With SwiftMailer I get this:
Connection could not be established with host smtp.gmail.com [ #0]
With PHPMailer, the message is:
SMTP Error: Could not connect to SMTP host.
My settings on SwiftMailer look like this:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername(APP_GMAIL_EMAIL)
->setPassword(APP_GMAIL_PWD);
I have gone through several threads here at SO and elsewhere, and tried many 'fixes' but the error messages remain the same. Some "solutions" I've tried are:
Enable OpenSSL inphp.ini (It was previously off)
Change smtp.gmail.com to smtp.googlemail.com
Replace smtp.gmail.com with certain gmail IP addresses
Replace smtp.gmail.com with gethostbyname('smtp.gmail.com')
Add ->setSourceIp('0.0.0.0') to the instantiation code above, after setUsername and setPassword
This code was previously working, and I'm not sure what could have broken the function. As part of troubleshooting, I have just confirmed that a similar code on Linux shared hosting is emailing satisfactorily, suggesting that the problem is somehow from my environment. Thus, I even tried different sources of internet access, in case it's an ISP thing.
How can I crack this nut?
I have been able to overcome the problem, at least with SwiftMailer. The solution must have arisen after my PHP upgrade from 5.2.5 to 5.6, which is the version I have currently. The insight came from this page:
https://github.com/swiftmailer/swiftmailer/issues/544
If you are using PHP 5.6, the error does occur because of the "SSL
context options" used for the stream context in swiftmailer. IN PHP
5.6 verify_peer and verify_peer_name the default was set to TRUE, so PHP checks the SSL certificate. It is currently not possible to
disable it in swiftmailer using some options.
You could disable the SSL check by modifying the function
"_establishSocketConnection" in StreamBuffer.php. Add these lines
before stream_socket_client command:
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
It would be great if these options could be set without hacking the
code.
Thanks to https://stackoverflow.com/a/29448735/2554788 who first pointed me to the said post.
By the way, the path to StreamBuffer.php is:
\lib\classes\Swift\Transport
A word of warning though: this solution is based on hacking the code inside a class, and will probably fail, say, after upgrading SwiftMailer versions (in which case you'd need to go back and hack the code again).
Perhaps, more current versions of SwiftMailer have put their acts together in PHP 5.6 (I use SwiftMailer 5.1 currently). I intend to try an upgrade ASAP, hoping there has been an elegant fix for this issue.

PHP mail() in dev environment: open the mail in a browser/editor instead of sending it?

On my machine I need to test the mails sent by my application. I'd rather avoid sending real mails.
Is there a way to have the email content showed to the screen a way or another, maybe by opening it in gedit or any text editor?
Maybe like replacing the commandline used to launch "sendmail"?
I am asking for Linux machines (Ubuntu more specifically).
Include a means of determining your environment in your project, or at least some kind of global variable that holds that information.
Then build an abstract mail interface that either sends real mails if it's running on a production server, but logs them to local files in case it runs on a dev machine / environment. As a logging package, I would recommend Monolog.
This would allow you to design the rest of your application (or at least the mail sending components) in a way that doesn't have to care about the environment.
After searching, here is the solution I came to:
create a script that will fake a smtp server
/usr/local/bin/sendmail-fake:
#!/bin/bash
{
date
echo $#
cat
} >> /var/log/sendmail-fake.log
configure PHP:
php.ini:
sendmail_path = /usr/local/bin/sendmail-fake
In this setup, emails are logged into a file. The script could be modified to open the content into a browser.
More details on the blog post.

Error using MIME::lite

I dont know if this error is specific to MIME::lite but I am getting it while I am trying to use smtp email with MIME.
No SASL mechanism found
at C:/Perl/site/lib/Authen/SASL.pm line 77
at C:/Perl/lib/Net/SMTP.pm line 143
What does this error mean?
I would guess the smtp server you're trying to contact is configured to use authentication of some sort and MIME::Lite is trying to use SASL (http://asg.web.cmu.edu/sasl/) under the hood. At least something is triggering SASL and you don't seem to have the right perl modules for it. The documentation for Authen::SASL mentions Authen::SASL::XS and Authen::SASL::Perl depending on your setup, the latter being pure perl and probably easier to run on windows. Below these two are the actual mechanism implementations e.g. Authen::SASL::Perl::CRAM_MD5 which also need to be in place.
It would be easier to debug if you included some initialization code for the libraries.