Skip to content

Instantly share code, notes, and snippets.

@bellerbrock
Last active December 6, 2019 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellerbrock/2248f83417fb71ca712fa10025a33bee to your computer and use it in GitHub Desktop.
Save bellerbrock/2248f83417fb71ca712fa10025a33bee to your computer and use it in GitHub Desktop.
Sending confirmation emails via SMTP

Notes about confirmation email troubleshooting.

Confirmation emails sent with the local hostname go to spam, and Mail::Sendmail does seem to support SMTP auth very well. I could not get Mail::Sendmail to work with smtp.gmail.com. I also had no luck with Email::Sender::Transport::SMTP which is already a dependency, or Email::Sender::Transport::SMTPS

The best soultion I've found is Email::Send::SMTP::Gmail which works well and has bonus features like support for file attachments. To use it, install it (cpanm Email::Send::SMTP::Gmail) and make sure the gmail account being used for login has the 'allow access for less secure apps' setting turned on.

Here's a test script for proof of concept:

#!/usr/bin/perl                                                                                                                        
use strict;
use warnings;

use Email::Send::SMTP::Gmail;

my $smtp_server = 'smtp.gmail.com';
my $port = 465;
my $login = "hfglinuxserver@gmail.com";
my $password = '<password>';

my ($mail,$error) = Email::Send::SMTP::Gmail->new(
    -layer =>'ssl',
    -port  => $port,
    -smtp  => $smtp_server,
    -login => $login,
    -pass  => $password
);

die "session error: $error" if $mail ==-1;

my $host = "http://192.168.42.173";
my $username = "smtest";
my $confirm_code = "2342jkhkgh;qr2345k1h235kjr";
my $project_name = "Breedbase";

my $subject = "[$project_name] E-mail Address Confirmation Request";
my $body = "Please do *NOT* reply to this message. The return address is not valid.                                                   
    Use the contact form instead ($host/contact/form).                                                                                
    This message is sent to confirm the email address for community user                                                              
    '$username'                                                                                                                       
                                                                                                                                      
    Please click (or cut and paste into your browser) the following link to                                                           
    confirm your account and email address:                                                                                           \
                                                                                                                                       
    $host/solpeople/account-confirm.pl?username=$username&confirm=$confirm_code                                                       \
                                                                                                                                                                                                                                                              
    Thank you,                                                                                                                        
    $project_name Team";

$mail->send(
    -to         => 'bellerbrock@gmail.com',
    -subject    => $subject,
    -body       => $body,
   # -attachments=>'test.pdf'                                                                                                          
);

$mail->bye;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment