SMTP to O365 using OAUTH2 and PHPMailer - oauth-2.0

I’m converting an existing PHPMailer app with Basic (userid and password) Authentication to use OAUTH2. Setup was easy but the app fails on authentication.
2020-05-23 15:14:59 Connection: opening to smtp.office365.com:587, timeout=300, options=array()
2020-05-23 15:14:59 Connection: opened
2020-05-23 15:14:59 SMTP INBOUND: "220 LO2P265CA0358.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 23 May 2020 15:14:58 +0000"
2020-05-23 15:14:59 SERVER -> CLIENT: 220 LO2P265CA0358.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 23 May 2020 15:14:58 +0000
2020-05-23 15:14:59 CLIENT -> SERVER: EHLO [my domain name]
2020-05-23 15:14:59 SMTP INBOUND: "250-LO2P265CA0358.outlook.office365.com Hello [91.208.99.2]"
2020-05-23 15:14:59 SMTP INBOUND: "250-SIZE 157286400"
2020-05-23 15:14:59 SMTP INBOUND: "250-PIPELINING"
2020-05-23 15:14:59 SMTP INBOUND: "250-DSN"
2020-05-23 15:14:59 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2020-05-23 15:14:59 SMTP INBOUND: "250-STARTTLS"
2020-05-23 15:14:59 SMTP INBOUND: "250-8BITMIME"
2020-05-23 15:14:59 SMTP INBOUND: "250-BINARYMIME"
2020-05-23 15:14:59 SMTP INBOUND: "250-CHUNKING"
2020-05-23 15:14:59 SMTP INBOUND: "250 SMTPUTF8"
2020-05-23 15:14:59 SERVER -> CLIENT: 250-LO2P265CA0358.outlook.office365.com Hello [91.208.99.2]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2020-05-23 15:14:59 CLIENT -> SERVER: STARTTLS
2020-05-23 15:14:59 SMTP INBOUND: "220 2.0.0 SMTP server ready"
2020-05-23 15:14:59 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2020-05-23 15:14:59 CLIENT -> SERVER: EHLO [my domain name]
2020-05-23 15:14:59 SMTP INBOUND: "250-LO2P265CA0358.outlook.office365.com Hello [91.208.99.2]"
2020-05-23 15:14:59 SMTP INBOUND: "250-SIZE 157286400"
2020-05-23 15:14:59 SMTP INBOUND: "250-PIPELINING"
2020-05-23 15:14:59 SMTP INBOUND: "250-DSN"
2020-05-23 15:14:59 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2020-05-23 15:14:59 SMTP INBOUND: "250-AUTH LOGIN XOAUTH2"
2020-05-23 15:14:59 SMTP INBOUND: "250-8BITMIME"
2020-05-23 15:14:59 SMTP INBOUND: "250-BINARYMIME"
2020-05-23 15:14:59 SMTP INBOUND: "250-CHUNKING"
2020-05-23 15:14:59 SMTP INBOUND: "250 SMTPUTF8"
2020-05-23 15:14:59 SERVER -> CLIENT: 250-LO2P265CA0358.outlook.office365.com Hello [91.208.99.2]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2020-05-23 15:14:59 Auth method requested: XOAUTH2
2020-05-23 15:14:59 Auth methods available on the server: LOGIN,XOAUTH2
2020-05-23 15:14:59 CLIENT -> SERVER: AUTH XOAUTH2 [long string of chars ending in == - base 64 encoded?]
2020-05-23 15:15:04 SMTP INBOUND: "535 5.7.3 Authentication unsuccessful [LO2P265CA0358.GBRP265.PROD.OUTLOOK.COM]"
2020-05-23 15:15:04 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [LO2P265CA0358.GBRP265.PROD.OUTLOOK.COM]
2020-05-23 15:15:04 SMTP ERROR: AUTH command failed: 535 5.7.3 Authentication unsuccessful [LO2P265CA0358.GBRP265.PROD.OUTLOOK.COM]
SMTP Error: Could not authenticate.
2020-05-23 15:15:04 CLIENT -> SERVER: QUIT
My composer.json is just:
"require": {
"phpmailer/phpmailer": "~6.1",
"stevenmaguire/oauth2-microsoft": "2.2.0"
which then also drags in the League oauth2-client and others.
To avoid the issue of OAUTH2 only working for free consumer accounts and not O365 (see Issue 3 - stevenmaguire/oauth2-microsoft on github - and I have replicated the issue to confirm it), I have edited /src/Provider/Microsoft.php as follows:
protected $urlAuthorize = 'https://login.microsoftonline.com/common/oauth2/authorize';
protected $urlAccessToken = 'https://login.microsoftonline.com/common/oauth2/token';
(This uses the Graph V1 endpoint. Note that the latest Graph endpoint V2 is accessed via ... oauth2/v2.0/token and ...oauth2/v2.0/authorize and that either of these uses different Scope parameters that are incompatible with the old 'wl' Windows Live ones)
My relevant PHPMailer code is:
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'XOAUTH2'; // omit this to send using basic authentication
$username = [my email address - as Azure AD signin];
$clientId = [from Azure AD - redacted];
$clientSecret = ‘[from Azure AD - redacted];
$refreshToken = [long char string – from running get_auth_token];
$provider = new Microsoft (
[
'clientId' => $clientId ,
'clientSecret' => $clientSecret
]
);
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken’,
'userName' =>$username
]
)
);
//$mail->Username = blah; // SMTP username – not needed for OAUTH2
//$mail->Password = blah; // SMTP password – not needed for OAUTH2
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
My get_oauth_token.php contains:
$clientId = [my client ID];
$clientSecret = [my client secret]';
$redirectUri = [the full URI of get_oauth_token.php];
I have created an app in MSFT Azure AD of type ‘Web app’, taken the client ID and client secret from there, and added API permissions for SMTP.Send, Mail.Send, offline_access, and openid
If I can find where in the code the Azure AD authentication codes and messages (vide https://learn.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes#lookup-current-error-code-information) are returned, I'll be a step forward! I have tried the AD Monitor for sign-ins, but these particular unsuccessful attempts are not logged - nor the successful getting of the refresh token.

The problem was eventually traced to MSFT's quirky implementation of OIDC/OAuth2 authentication that I know from posts on Stackoverflow and elsewhere has confused many developers. In effect MSFT had not completed the job.
I have described this - and the solution - at some length in https://github.com/decomplexity/SendOauth2/blob/main/MSFT%20OAuth2%20quirks.md

$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'LOGIN'; // omit this to send using basic authentication
$mail->Username = 'sdfdsfds#outlook.com'; // SMTP username – not needed for OAUTH2
$mail->Password = 'fdssdf'; // SMTP password – not needed for OAUTH2
$mail->SMTPSecure = 'starttls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
Be careful with phpmailer, I was using 5.2 and in class.smtp it was encoding usermail twice, so my solution was delete that base64_encode in following CASE:
case 'LOGIN':
// Start authentication
if (!$this->sendCommand('AUTH', 'AUTH LOGIN', 334)) {
return false;
}
if (!$this->sendCommand("Username", ($username), 334)) {
return false;
}
if (!$this->sendCommand("Password", base64_encode($password), 235)) {
return false;
}
break;

Related

JavaMail and Oauth2: Exception "Can't send command to SMTP host" / "SSLHandshakeException: No appropriate protocol"

I get the following exception when trying to send a mail with JavaMail with OAuth2:
javax.mail.MessagingException: Can't send command to SMTP host (javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate))
I have already checked dozens of stackOverFlow posts and none of the solutions work.
This is my javamail config (commented out some other configs I have tried):
...
String oauth2_access_token = <procedure to aquire a token>;
Properties props = new Properties();
props.put("mail.host",config.getString("MAILSERVER"));
props.put("mail.smtp.port", config.getString("MAILPORT"));
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.1 TLSv1.2 TLSv1.3");
//props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
//props.put("mail.smtp.auth.xoauth2.disable", false);
//props.put("mail.smtp.sasl.enable", "true");
//props.put("mail.smtp.auth.login.disable","true");
//props.put("mail.smtp.auth.plain.disable","true");
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");
// Connect
javax.mail.Session mailSession = javax.mail.Session.getInstance(props);
mailSession.setDebug(true);
SMTPTransport transport = (SMTPTransport) mailSession.getTransport("smtp");
transport.connect(config.getString("MAILSERVER"),
Integer.parseInt(config.getString("MAILPORT")), config.getString("MAILUSER"), oauth2_access_token);
Here is some output:
[apache-tomcat-9.0.54]: DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
[apache-tomcat-9.0.54]: 220 SOMEANONYMIZEDSERVERPREFIX.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 16 Mar 2022 10:16:37 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587
[apache-tomcat-9.0.54]: EHLO MY_COMPUTER_NAME
[apache-tomcat-9.0.54]: 250-SOMEANONYMIZEDSERVERPREFIX.outlook.office365.com Hello [MY_IP_ADRESS]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
STARTTLS
[apache-tomcat-9.0.54]: 220 2.0.0 SMTP server ready
[apache-tomcat-9.0.54]: EHLO MY_COMPUTER_NAME
[apache-tomcat-9.0.54]: ERROR 2022-03-16 11:16:39,407 [..MyProgramException..] - javax.mail.MessagingException: Can't send command to SMTP host (javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate))
I think it has to do with issuing the AUTH command because it is not listed in output. The connection over TLS is established successfully so I think the SSLHandshake Exception might be misleading. So what gives?
Might it have to do with the token? The scope I had to use for the token aquisition is ".default". "Mail.Send" didn't work.
I am using the newest JavaMail version 1.6.2 and AdoptOpenJdk 11.0.12.
I also double checked java.security config. TLS 1v2 and 1v3 algorithms are not disabled.
I found the solution myself. Maybe somone can make use of it:
The code above was not false to my knowledge. The actual reason was, that I should not have used a token acquisition with client credentials but only with username/password and client id.
I am not sure if JavaMail supports this because in the documentation I could not find anything about it. The rare examples about this are usually with client credentials.
As you can see above, no AUTH command was triggered because I had not the permission to do it obviously.
What I needed to do aswell was to use the Microsoft Graph API not only to aquire a token via "password/username provider" (this type) instead of the "client credential provider" (link) but also to actually trigger a send mail command via Microsoft Graph API (link).
The client credential provider was in my case not sufficient because the admin had not specified permissions to send mails.
Thus, I didn't use JavaMail anymore for this and needed another token acquisition provider.

Jenkins : getting error when we try to send notification from Jenkins : error "550 5.5.0 Invalid EHLO/HELO domain"

Jenkins : getting error when we try to send notification from Jenkins : error "550 5.5.0 Invalid EHLO/HELO domain"
Check the full error :
Not sent to the following valid addresses: prakashranasinghe555#gmail.com admin
SMTPSendFailedException message: 550 5.5.0 Invalid EHLO/HELO domain.
Next SMTPSenderFailedException message: 550 5.5.0 Invalid EHLO/HELO domain.
QUIT
221 2.0.0 closing connection u13sm8039654pfi.51 - gsmtp
Finished: SUCCESS
++++++++++
Checking for post-build
Performing post-build step
Checking if email needs to be generated
Email was triggered for: Always
Sending email for trigger: Always
Sending mail from default account using System Admin e-mail address
messageContentType = text/plain; charset=UTF-8
Collecting change authors...
build: 32
Adding recipients from project recipient list
Analyzing: prakashranas555#gmail.com
Looking for: prakashranasinghe#gmail.com
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: prakashranasinghe555#gmail.com
=> found type: 0
Analyzing: prakashranasinghe555#gmail.com
Looking for: prakashranasinghe555#gmail.com
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: prakashranasinghe555#gmail.com
=> found type: 0
Analyzing: prakashranasinghe555#gmail.com
Looking for: prakashranasinghe555#gmail.com
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: prakashranasinghe555#gmail.com
=> found type: 0
Adding admin with address admin
Analyzing: admin
Looking for: admin
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: admin
=> found type: 0
Analyzing: admin
Looking for: admin
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: admin
=> found type: 0
Analyzing: admin
Looking for: admin
starting at: 0
firstFoundIdx: 0
firstFoundIdx-substring: admin
=> found type: 0
Adding recipients from trigger recipient list
Successfully created MimeMessage
Sending email to: prakashranasinghe555#gmail.com admin
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
220 smtp.gmail.com ESMTP u13sm8039654pfi.51 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 25
EHLO Prakashr.Direct.local
250-smtp.gmail.com at your service, [123.231.87.10]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<nobody#nowhere>
550 5.5.0 Invalid EHLO/HELO domain.
DEBUG SMTP: got response code 550, with response: 550 5.5.0 Invalid EHLO/HELO domain.
RSET
250 2.1.5 Flushed u13sm8039654pfi.51 - gsmtp
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 550 5.5.0 Invalid EHLO/HELO domain.
;
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 5.5.0 Invalid EHLO/HELO domain.
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
at hudson.plugins.emailext.ExtendedEmailPublisher.sendMail(ExtendedEmailPublisher.java:539)
at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:444)
at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:354)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:21)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:808)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:757)
at hudson.model.Build$BuildExecution.cleanUp(Build.java:189)
at hudson.model.Run.execute(Run.java:1958)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:100)
at hudson.model.Executor.run(Executor.java:433)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 5.5.0 Invalid EHLO/HELO domain.
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1817)
... 12 more
Not sent to the following valid addresses: prakashranasinghe555#gmail.com admin
SMTPSendFailedException message: 550 5.5.0 Invalid EHLO/HELO domain.
Next SMTPSenderFailedException message: 550 5.5.0 Invalid EHLO/HELO domain.
QUIT
221 2.0.0 closing connection u13sm8039654pfi.51 - gsmtp
Finished: SUCCESS
According to an answer at ServerFault to SMTP Server 550 Access denied - Invalid HELO name it's probably the line:
EHLO Prakashr.Direct.local
which doesn't have an FQDN.

Error While Sending Extended email notification from Jenkins

i have configured extended email notification in Jenkins configure system, and created a new job and configured editable email notification, once the build is completed, email is not sent and getting the following in console Not sent to the following valid addresses: user#domain.com
and here is the debug log
RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 451 5.7.3 STARTTLS is required to send mail [PN********6.INDPRD01.PROD.OUTLOOK.COM]
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
at hudson.plugins.emailext.ExtendedEmailPublisher.sendMail(ExtendedEmailPublisher.java:541)
at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:446)
at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:354)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:803)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:752)
at hudson.model.Build$BuildExecution.cleanUp(Build.java:187)
at hudson.model.Run.execute(Run.java:1954)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Not sent to the following valid addresses: user#domain.com
SMTPSendFailedException message: 451 5.7.3 STARTTLS is required to send mail [P******06.INDPRD01.PROD.OUTLOOK.COM]
QUIT
DEBUG SMTP: EOF: [EOF]
Here is the screenshot how i configured email notification in Configure System:
Just configure this in Jenkins options file:
JENKINS_JAVA_OPTIONS : -Djava.awt.headless=true -Dmail.smtp.starttls.enable=true
JENKINS_OPTS : -Dmail.smtp.starttls.enable=true
Add this Java argument in Jenkins.xml file for Windows or in /etc/default/jenkins for Ubuntu:
-Dmail.smtp.starttls.enable=true
Alternatively, you can try to change the SMTP port from 587 to 465. Use SSL should be true in this case.
Reference:
Jenkins SMTP TLS
https://superuser.com/questions/879361/how-to-configure-jenkins-email-notifications-through-outlook

GoDaddy Office 365 OAuth2 for SMTP fails with Authentication unsuccessful

Following this guide on OAuth2 support for IMAP/SMTP, I was able to retrieve the access token and authenticate IMAP and SMTP fine, but for some Office 365 accounts like the ones that were bought from GoDaddy, AUTH XOAUTH2 command returns Authentication unsuccessful as shown in logs below.
Connecting to 'smtp.office365.com:587', SSL/TLS: False.
S: 220 AM7PR02CA0017.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 14 Aug 2020 12:24:06 +0000
C: EHLO [127.0.0.1]
S: 250-AM7PR02CA0017.outlook.office365.com Hello [79.185.54.24]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-8BITMIME
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [127.0.0.1]
S: 250-AM7PR02CA0017.outlook.office365.com Hello [79.185.54.24]
S: 250-SIZE 157286400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH LOGIN XOAUTH2
S: 250-8BITMIME
S: 250 SMTPUTF8
C: AUTH XOAUTH2 *PASSWORD*
S: 535 5.7.3 Authentication unsuccessful [AM7PR02CA0017.eurprd02.prod.outlook.com]
URLs below are used for authorization:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize
https://login.microsoftonline.com/common/oauth2/v2.0/token
Scope for SMTP used: https://outlook.office.com/SMTP.Send (as documented in the guide from Microsoft). In the Azure portal, it is added in Permissions API under Graph API (since it is not available under Exchange anymore).
The app is registered for both personal Outlook.com users and Office 365 organizations in Azure (that is why common is used as a tenant in auth URLs).
It works for our developer license Office 365 organization but for the one that is bought through GoDaddy (on https://www.godaddy.com/email/professional-business-email) it fails to authenticate SMTP (IMAP works fine).
Things already tried:
Limiting scopes to the ones related to Exchange resource (as suggested in https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth)
Changing tenant to organizations in auth URLs (instead of common)
Using https://outlook.office365.com/SMTP.Send as scope for SMTP
Any suggestions?

Connection refused - connect(2) for "localhost" port 25 chef devops

I am working on chef(devops) where I have a helper library with the following code in it
require 'net/smtp'
module HandlerSendEmail
class Helper
def send_email_on_run_failure(node_name)
message = "From: Chef <chef#chef.io>\n"
message << "To: Grant <xyz#test.com>\n"
message << "Subject: Chef run failed\n"
message << "Date: #{Time.now.rfc2822}\n\n"
message << "Chef run failed on #{node_name}\n"
Net::SMTP.start('localhost', 25) do |smtp|
smtp.send_message message, 'chef#chef.io', 'xyz#test.com'
end
end
end
end
But whhen I run the recipe I get
Chef Client failed. 0 resources updated in 02 seconds
[2017-10-30T05:19:38+00:00] ERROR: Connection refused - connect(2) for "localhost" port 25
[2017-10-30T05:19:38+00:00] ERROR: Connection refused - connect(2) for "localhost" port 25
I tried changing port to 90 and some other options I keep getting same error.There are some solution available on few posts on stackoverflow already but all of them are talking about some other .rb files which is not present on my dev environment.
Connection refused - connect(2) Ruby on Rails Mail Setup
Errno::ECONNREFUSED: Connection refused - connect(2) for action mailer
You would need to have an actual SMTP server listening on localhost and I'm guessing you don't have one. Without more information it's hard to say though. If you don't want run your own relay server, you can find instructions all over the internet for setting up outbound SMTP via GMail, Amazon SES, Sparkpost, Sendgrid, and may more.

Categories

Resources