| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package Trog::Email;
- use 5.040;
- use re '/aasx';
- use Email::MIME;
- use Email::Sender::Simple;
- use Trog::Auth;
- use Trog::Log qw{:all};
- use Trog::Renderer;
- =head1 Trog::Email
- Sending mail out of tCMS.
- =head1 Termination Conditions
- Dies when asked to mail a user who has no contact email set on their profile.
- Failure to actually send is logged as FATAL rather than thrown, as by then the
- message has already been built and there's nothing the caller can do about it.
- =head1 FUNCTIONS
- =head2 contact(STRING user, STRING from, STRING subject, HASHREF data) = BOOL
- Send a templated message to a user's contact email.
- $data must have a 'template' key naming the template to render; everything else
- in it is passed through to that template as its data. The template is rendered
- twice, once as text and once as HTML, and both are attached to a multipart
- message so that the recipient's client can pick whichever it prefers.
- Returns 1.
- =cut
- sub contact ( $user, $from, $subject, $data ) {
- my $email = Trog::Auth::email4user($user);
- die "No contact email set for user $user!" unless $email;
- my $render = Trog::Renderer->render(
- contenttype => 'multipart/related',
- code => 200,
- template => $data->{template},
- data => {
- method => 'EMAIL',
- # Important - this will prevent caching
- route => '',
- %$data,
- },
- );
- my $text = $render->{text}[2][0];
- my $html = $render->{html}[2][0];
- my @parts = (
- Email::MIME->create(
- attributes => {
- content_type => "text/plain",
- disposition => "attachment",
- charset => 'UTF-8',
- },
- body => $text,
- ),
- Email::MIME->create(
- attributes => {
- content_type => "text/html",
- disposition => "attachment",
- charset => "UTF-8",
- },
- body => $html,
- ),
- );
- my $mail = Email::MIME->create(
- header_str => [
- From => $from,
- To => [$email],
- Subject => $subject,
- ],
- parts => \@parts,
- );
- Email::Sender::Simple->try_to_send($mail) or do {
- FATAL("Could not send email from $from to $email!");
- };
- return 1;
- }
- 1;
|