{"id":4491,"date":"2025-04-30T11:31:14","date_gmt":"2025-04-30T11:31:14","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4491"},"modified":"2025-04-30T11:31:56","modified_gmt":"2025-04-30T11:31:56","slug":"perl-mailmailer-from-address-being-ignored","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4491","title":{"rendered":"Perl Mail::Mailer From Address, being ignored?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">While using Perl Mail::Mailer to send from an on-site application to our Microsoft 365 tenancy via SMTP, we had an oddity. We&#8217;d already setup the Microsoft 365 tenancy to have a receive connector which was autheniticating via the source address, i.e. our on-premise IP range. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When testing via a test Perl script which was the same as the configuration used within the on-site application we hit a problem with it sending. The example Perl script <strong>test-email.pl<\/strong> is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Mail::Mailer;\nmy $m = Mail::Mailer->new( 'smtp', 'Server' => 'blah.mail.protection.outlook.com', 'Debug' => 1,\n  'Hello' => 'mydomain.com');\n$m->open({ 'From'    => 'tristan@mydomain.com',\n           'Subject' => 'TEST EMAIL',\n           'Reply-To' => 'tristan@mydomain.com',\n           'To'      => 'recipient@anotherdomain.com' } );\nprint {$m} \"This is a test\";\n$m->close;\nprint \"$!\\n\";<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If using telnet to send and manually typing in the email it worked fine. But via the Perl script the following error was being given by Microsoft&#8217;s server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Net::SMTP=GLOB(0x55634f206978)&lt;&lt;&lt; 451 4.4.62 Mail sent to the wrong Office 365 region. ATTR35. For more information please go to https:\/\/go.microsoft.com\/fwlink\/?linkid=865268 &#91;No TLS] &#91;CW2PEPF00                                          0056BA.GBRP265.PROD.OUTLOOK.COM 2025-04-30T10:52:28.302Z 08DD86E66AB3BF99]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After some troubleshooting and analysing the debug logs, we noticed the &#8220;MAIL FROM&#8221; was being ignored for some reason, it was defaulting to send from some default address for the host, rather than the FROM address we specified within the script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*** ADDR>> root@server123.internal.mydomain.com at \/usr\/share\/perl\/5.34\/Net\/SMTP.pm line 298.\n*** ADDR&lt;&lt; root@server123.internal.mydomain.com> at \/usr\/share\/perl\/5.34\/Net\/SMTP.pm line 316.\n<strong>Net::SMTP=GLOB(0x55634f206978)>>> MAIL FROM:&lt;root@server123.internal.testing.com><\/strong>\nNet::SMTP=GLOB(0x55634f206978)&lt;&lt;&lt; 250 2.1.0 Sender OK\n*** ADDR>> recipient@anotherdomain.com at \/usr\/share\/perl\/5.34\/Net\/SMTP.pm line 298.\nNet::SMTP=GLOB(0x55634f206978)>>> RCPT TO:&lt;recipient@anotherdomain.com>\nNet::SMTP=GLOB(0x55634f206978)&lt;&lt;&lt; 451 4.4.62 Mail sent to the wrong Office 365 region. ATTR35. For more information please go to https:\/\/go.microsoft.com\/fwlink\/?linkid=865268 &#91;No TLS] &#91;CW2PEPF00                                          0056BA.GBRP265.PROD.OUTLOOK.COM 2025-04-30T10:52:28.302Z 08DD86E66AB3BF99]\nNet::SMTP=GLOB(0x55634f206978)>>> DATA\nNet::SMTP=GLOB(0x55634f206978)&lt;&lt;&lt; 503 5.5.2 Need rcpt command &#91;CW2PEPF000056BA.GBRP265.PROD.OUTLOOK.COM 2025-04-30T10:52:28.308Z 08DD86E66AB3BF99]\nNet::SMTP=GLOB(0x55634f206978)>>> Subject: TEST EMAIL\nNet::SMTP=GLOB(0x55634f206978)>>> Reply-To: tristan@mydomain.com\nNet::SMTP=GLOB(0x55634f206978)>>> From: tristan@mydomain.com\nNet::SMTP=GLOB(0x55634f206978)>>> X-Mailer: Mail::Mailer&#91;v2.21] Net::SMTP&#91;v3.13]\nNet::SMTP=GLOB(0x55634f206978)>>> To: recipient@anotherdomain.com\nNet::SMTP=GLOB(0x55634f206978)>>> This is a test\nNet::SMTP=GLOB(0x55634f206978)>>> .\nNet::SMTP=GLOB(0x55634f206978)&lt;&lt;&lt; 500 5.3.3 Unrecognized command 'Length: 8' &#91;CW2PEPF000056BA.GBRP265.PROD.OUTLOOK.COM 2025-04-30T10:52:28.314Z 08DD86E66AB3BF99]\nNet::SMTP=GLOB(0x55634f206978)>>> QUIT<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So now we can see why the email submission is failing, the question was &#8220;why was Mail::Mailer ignoring the FROM address specified in the $m (message) object?&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The resolution was to add the &#8220;from&#8221; into the original creation of the $m (message) object, i.e. as below, so both including it within that line and in the body of the message so to speak.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Mail::Mailer;\nmy $m = Mail::Mailer->new( 'smtp', 'Server' => 'blah.mail.protection.outlook.com', 'Debug' => 1,\n  'Hello' => 'mydomain.com', <strong>'From' => 'tristan@mydomain.com'<\/strong> );\n$m->open({ 'From'    => 'tristan@mydomain.com',\n           'Subject' => 'TEST EMAIL',\n           'Reply-To' => 'tristan@mydomain.com',\n           'To'      => 'recipient@anotherdomain.com' } );\nprint {$m} \"This is a test\";\n$m->close;\nprint \"$!\\n\";<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now when testing and using the debug logs, the MAIL FROM: is being used as specified, rather than the server default one (which Microsoft 365 rejects because it didn&#8217;t contain our valid sender domain) meaning the correct from address was used and the message was accepted as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While using Perl Mail::Mailer to send from an on-site application to our Microsoft 365 tenancy via SMTP, we had an oddity. We&#8217;d already setup the Microsoft 365 tenancy to have a receive connector which was autheniticating via the source address, i.e. our on-premise IP range. When testing via a test Perl script which was the &#8230; <a title=\"Perl Mail::Mailer From Address, being ignored?\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4491\" aria-label=\"Read more about Perl Mail::Mailer From Address, being ignored?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4384,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-4491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-random"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4491"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4491\/revisions"}],"predecessor-version":[{"id":4492,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4491\/revisions\/4492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4384"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}