... for my $email (@mail) { my $attached = ($code) ? "attach_path => '$file_name',attach_filename => '$file_name',attach_type => 'application/txt'," : ''; my $success = email( { to => $email, from => $from, from_name => $name, subject => $subject, body => $body, $attached } ); } ...