| 1 |
NAME |
| 2 |
Catalyst::View::Email - Send Email from Catalyst |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
This module sends out emails from a stash key specified in the |
| 6 |
configuration settings. |
| 7 |
|
| 8 |
CONFIGURATION |
| 9 |
WARNING: since version 0.10 the configuration options slightly changed! |
| 10 |
|
| 11 |
Use the helper to create your View: |
| 12 |
|
| 13 |
$ script/myapp_create.pl view Email Email |
| 14 |
|
| 15 |
In your app configuration: |
| 16 |
|
| 17 |
__PACKAGE__->config( |
| 18 |
'View::Email' => { |
| 19 |
# Where to look in the stash for the email information. |
| 20 |
# 'email' is the default, so you don't have to specify it. |
| 21 |
stash_key => 'email', |
| 22 |
# Define the defaults for the mail |
| 23 |
default => { |
| 24 |
# Defines the default content type (mime type). Mandatory |
| 25 |
content_type => 'text/plain', |
| 26 |
# Defines the default charset for every MIME part with the |
| 27 |
# content type text. |
| 28 |
# According to RFC2049 a MIME part without a charset should |
| 29 |
# be treated as US-ASCII by the mail client. |
| 30 |
# If the charset is not set it won't be set for all MIME parts |
| 31 |
# without an overridden one. |
| 32 |
# Default: none |
| 33 |
charset => 'utf-8' |
| 34 |
}, |
| 35 |
# Setup how to send the email |
| 36 |
# all those options are passed directly to Email::Sender::Simple |
| 37 |
sender => { |
| 38 |
# if mailer doesn't start with Email::Sender::Simple::Transport::, |
| 39 |
# then this is prepended. |
| 40 |
mailer => 'SMTP', |
| 41 |
# mailer_args is passed directly into Email::Sender::Simple |
| 42 |
mailer_args => { |
| 43 |
host => 'smtp.example.com', # defaults to localhost |
| 44 |
sasl_username => 'sasl_username', |
| 45 |
sasl_password => 'sasl_password', |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
NOTE ON SMTP |
| 52 |
If you use SMTP and don't specify host, it will default to localhost and |
| 53 |
attempt delivery. This often means an email will sit in a queue and not |
| 54 |
be delivered. |
| 55 |
|
| 56 |
SENDING EMAIL |
| 57 |
Sending email is just filling the stash and forwarding to the view: |
| 58 |
|
| 59 |
sub controller : Private { |
| 60 |
my ( $self, $c ) = @_; |
| 61 |
|
| 62 |
$c->stash->{email} = { |
| 63 |
to => '[email protected]', |
| 64 |
cc => '[email protected]', |
| 65 |
from => '[email protected]', |
| 66 |
subject => 'I am a Catalyst generated email', |
| 67 |
body => 'Body Body Body', |
| 68 |
}; |
| 69 |
|
| 70 |
$c->forward( $c->view('Email') ); |
| 71 |
} |
| 72 |
|
| 73 |
Alternatively you can use a more raw interface and specify the headers |
| 74 |
as an array reference like it is passed to Email::MIME::Creator. Note |
| 75 |
that you may also mix both syntaxes if you like ours better but need to |
| 76 |
specify additional header attributes. The attributes are appended to the |
| 77 |
header array reference without overwriting contained ones. |
| 78 |
|
| 79 |
$c->stash->{email} = { |
| 80 |
header => [ |
| 81 |
To => '[email protected]', |
| 82 |
Cc => '[email protected]', |
| 83 |
Bcc => join ',', qw/[email protected] [email protected]/, |
| 84 |
From => '[email protected]', |
| 85 |
Subject => 'Note the capitalization differences', |
| 86 |
], |
| 87 |
body => qq{Ain't got no body, and nobody cares.}, |
| 88 |
# Or, send parts |
| 89 |
parts => [ |
| 90 |
Email::MIME->create( |
| 91 |
attributes => { |
| 92 |
content_type => 'text/plain', |
| 93 |
disposition => 'attachment', |
| 94 |
charset => 'US-ASCII', |
| 95 |
}, |
| 96 |
body => qq{Got a body, but didn't get ahead.}, |
| 97 |
) |
| 98 |
], |
| 99 |
}; |
| 100 |
|
| 101 |
You can set the envelope sender and recipient as well: |
| 102 |
|
| 103 |
$c->stash->{email} = { |
| 104 |
|
| 105 |
envelope_from => '[email protected]', |
| 106 |
from => '[email protected]', |
| 107 |
|
| 108 |
envelope_to => [ '[email protected]', '[email protected]' ], |
| 109 |
to => 'Undisclosed Recipients:;', |
| 110 |
|
| 111 |
... |
| 112 |
}; |
| 113 |
|
| 114 |
HANDLING ERRORS |
| 115 |
If the email fails to send, the view will die (throw an exception). |
| 116 |
After your forward to the view, it is a good idea to check for errors: |
| 117 |
|
| 118 |
$c->forward( $c->view('Email') ); |
| 119 |
|
| 120 |
if ( scalar( @{ $c->error } ) ) { |
| 121 |
$c->error(0); # Reset the error condition if you need to |
| 122 |
$c->response->body('Oh noes!'); |
| 123 |
} else { |
| 124 |
$c->response->body('Email sent A-OK! (At least as far as we can tell)'); |
| 125 |
} |
| 126 |
|
| 127 |
USING TEMPLATES FOR EMAIL |
| 128 |
Now, it's no fun to just send out email using plain strings. Take a look |
| 129 |
at Catalyst::View::Email::Template to see how you can use your favourite |
| 130 |
template engine to render the mail body. |
| 131 |
|
| 132 |
METHODS |
| 133 |
new Validates the base config and creates the Email::Sender::Simple |
| 134 |
object for later use by process. |
| 135 |
|
| 136 |
process($c) |
| 137 |
The process method does the actual processing when the view is |
| 138 |
dispatched to. |
| 139 |
|
| 140 |
This method sets up the email parts and hands off to |
| 141 |
Email::Sender::Simple to handle the actual email delivery. |
| 142 |
|
| 143 |
setup_attributes($c, $attr) |
| 144 |
Merge attributes with the configured defaults. You can override this |
| 145 |
method to return a structure to pass into generate_message which |
| 146 |
subsequently passes the return value of this method to |
| 147 |
Email::MIME->create under the "attributes" key. |
| 148 |
|
| 149 |
generate_message($c, $attr) |
| 150 |
Generate a message part, which should be an Email::MIME object and |
| 151 |
return it. |
| 152 |
|
| 153 |
Takes the attributes, merges with the defaults as necessary and |
| 154 |
returns a message object. |
| 155 |
|
| 156 |
TROUBLESHOOTING |
| 157 |
As with most things computer related, things break. Email even more so. |
| 158 |
Typically any errors are going to come from using SMTP as your sending |
| 159 |
method, which means that if you are having trouble the first place to |
| 160 |
look is at Email::Sender::Transport::SMTP. This module is just a wrapper |
| 161 |
for Email::Sender::Simple, so if you get an error on sending, it is |
| 162 |
likely from there anyway. |
| 163 |
|
| 164 |
If you are using SMTP and have troubles sending, whether it is |
| 165 |
authentication or a very bland "Can't send" message, make sure that you |
| 166 |
have Net::SMTP and, if applicable, Net::SMTP::SSL installed. |
| 167 |
|
| 168 |
It is very simple to check that you can connect via Net::SMTP, and if |
| 169 |
you do have sending errors the first thing to do is to write a simple |
| 170 |
script that attempts to connect. If it works, it is probably something |
| 171 |
in your configuration so double check there. If it doesn't, well, keep |
| 172 |
modifying the script and/or your mail server configuration until it |
| 173 |
does! |
| 174 |
|
| 175 |
SEE ALSO |
| 176 |
Catalyst::View::Email::Template - Send fancy template emails with Cat |
| 177 |
Catalyst::Manual - The Catalyst Manual |
| 178 |
Catalyst::Manual::Cookbook - The Catalyst Cookbook |
| 179 |
AUTHORS |
| 180 |
J. Shirley <[email protected]> |
| 181 |
|
| 182 |
Alexander Hartmaier <[email protected]> |
| 183 |
|
| 184 |
CONTRIBUTORS |
| 185 |
(Thanks!) |
| 186 |
|
| 187 |
Matt S Trout |
| 188 |
|
| 189 |
Daniel Westermann-Clark |
| 190 |
|
| 191 |
Simon Elliott <[email protected]> |
| 192 |
|
| 193 |
Roman Filippov |
| 194 |
|
| 195 |
Lance Brown <[email protected]> |
| 196 |
|
| 197 |
Devin Austin <[email protected]> |
| 198 |
|
| 199 |
Chris Nehren <[email protected]> |
| 200 |
|
| 201 |
COPYRIGHT |
| 202 |
Copyright (c) 2007 - 2009 the Catalyst::View::Email "AUTHORS" and |
| 203 |
"CONTRIBUTORS" as listed above. |
| 204 |
|
| 205 |
LICENSE |
| 206 |
This library is free software, you can redistribute it and/or modify it |
| 207 |
under the same terms as Perl itself. |
| 208 |
|