Skip to content

Commit

Permalink
fix: use new HTML email template to welcome user
Browse files Browse the repository at this point in the history
Fixes #8005
  • Loading branch information
raphael0202 committed Jul 21, 2023
1 parent 409a32d commit c580763
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 61 deletions.
1 change: 1 addition & 0 deletions lang-default/en/emails/user_welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body><p>Welcome {{NAME}}, your user ID: <b>{{USERID}}</b></p></body></html>
74 changes: 73 additions & 1 deletion lib/ProductOpener/Mail.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ BEGIN {
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw(
&send_email
&send_html_email
&send_email_to_admin
&send_email_to_producers_admin
&get_html_email_content
$LOG_EMAIL_START
$LOG_EMAIL_END
Expand Down Expand Up @@ -127,7 +129,7 @@ sub _send_email ($mail) {

=head2 send_email( USER_REF, SUBJECT, TEXT )
C<send_email()> sends email from the contact email of Open Food Facts to the email passed as an argument.
C<send_email()> sends a plain text email from the contact email of Open Food Facts to the email passed as an argument.
=head3 Arguments
Expand Down Expand Up @@ -160,6 +162,76 @@ sub send_email ($user_ref, $subject, $text) {
return _send_email($mail);
}

=head2 send_html_email( USER_REF, SUBJECT, HTML_CONTENT )
C<send_html_email()> sends an HTML email from the contact email of Open Food Facts to the email passed as an argument.
=head3 Arguments
The first argument is a hash reference. The other two arguments are scalar variables that consist of email subject and HTML content.
=head4 Input keys: user data
The hash must contain values for the following keys:
- email -> user email
- name -> user name
=head3 Return Values
The function returns a 1 or 0 depending on the evaluation of the email sent or not.
If the function catches any error during evaluation it returns 1 indicating an error.
On the other hand, if there was no error, it returns 0 indicating that the email has been sent successfully.
=cut

sub send_html_email ($user_ref, $subject, $html_content) {
my $email = $user_ref->{email};
my $name = $user_ref->{name};

my $mail = Email::Stuffer->from(lang("site_name") . " <$contact_email>")->to($name . " <$email>")->subject($subject)
->html_body($html_content);
return _send_email($mail);
}

=head2 get_html_email_content ($filename, $lang )
Fetch the HTML email content in $DATA_ROOT/lang/emails. If a translation is available
for the requested language, we provide the translated version otherwise English is the default.
=head3 Arguments
=head4 $filename
The HTML file name (ex: user_welcome.html)
=head4 $lang
The 2-letter language code
=head3 Return Values
The HTML string or undef if the file does not exists or is not readable.
=cut

sub get_html_email_content ($filename, $lang) {
# if an email does not exist in the local language, use the English version
my $file = "$data_root/lang/$lang/emails/$filename";

if (!-e $file) {
$file = "$data_root/lang/en/emails/$filename";
}

open(my $IN, "<:encoding(UTF-8)", $file) or log->error("Can't open $file for reading");
return unless $IN;
my $html = join('', (<$IN>));
close($IN);
return $html;
}

=head1 FUNCTIONS
=head2 send_email_to_admin( SUBJECT, TEXT )
Expand Down
12 changes: 8 additions & 4 deletions lib/ProductOpener/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,14 @@ sub process_user_form ($type, $user_ref, $request_ref) {
param("user_id", $userid);
init_user($request_ref);

my $email = lang("add_user_email_body");
$email =~ s/<USERID>/$userid/g;
# $email =~ s/<PASSWORD>/$user_ref->{password}/g;
$error = send_email($user_ref, lang("add_user_email_subject"), $email);
# Fetch the HTML mail template corresponding to the user language, english is the
# default if the translation is not available
my $email_content = get_html_email_content("user_welcome.html", $user_ref->{initial_lc});
my $user_name = $user_ref->{name};
# Replace placeholders by user values
$email_content =~ s/\{\{USERID\}\}/$userid/g;
$email_content =~ s/\{\{NAME\}\}/$user_name/g;
$error = send_html_email($user_ref, lang("add_user_email_subject"), $email_content);

my $admin_mail_body = <<EMAIL
Expand Down
31 changes: 0 additions & 31 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,6 @@ msgctxt "f_this_is_a_pro_account_for_org"
msgid "This account is a professional account associated with the producer or brand {org}. You have access to the Platform for Producers."
msgstr "This account is a professional account associated with the producer or brand {org}. You have access to the Platform for Producers."

msgctxt "add_user_email_body"
msgid ""
"Hello <NAME>,\n"
"\n"
"Thanks a lot for joining https://openfoodfacts.org\n"
"Here is your user name:\n"
"\n"
"User name: <USERID>\n"
"\n"
"You can now sign in on the site to add and edit products.\n"
"\n"
"<<site_name>> is a collaborative project to which you can bring much more "
"than new products: your energy, enthusiasm and ideas!\n"
"To discuss between contributors and make the project go forward, we use a "
"very convenient messaging system called Slack: https://slack.openfoodfacts."
"org\n"
"\n"
"You can also join the Facebook group for contributors:\n"
"https://www.facebook.com/groups/OpenFoodFacts/\n"
"\n"
"New: we are also starting Open Beauty Facts to create a database of "
"cosmetics: soap, toothpaste, makeup etc.\n"
"https://openbeautyfacts.org\n"
"\n"
"Thank you very much!\n"
"\n"
"Stéphane and the Open Food Facts team\n"
"https://openfoodfacts.org\n"
"https://twitter.com/OpenFoodFacts\n"
msgstr ""

# please check that site_name and the brackets stays intact
msgctxt "add_user_email_subject"
msgid "Thanks for joining <<site_name>>"
Expand Down
28 changes: 3 additions & 25 deletions tests/integration/expected_test_results/create_pro_user/mails.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,35 +146,13 @@
[
"Date: ***",
"MIME-Version: 1.0",
"Content-Type: text/plain; charset=utf-8",
"Content-Type: text/html; charset=utf-8",
"Content-Transfer-Encoding: quoted-printable",
"From: \"Open Food Facts\" <[email protected]>",
"To: Test <[email protected]>",
"Subject: Thanks for joining Open Food Facts",
"",
"Hello Test,",
"",
"Thanks a lot for joining https://openfoodfacts.org",
"Here is your user name:",
"",
"User name: tests",
"",
"You can now sign in on the site to add and edit products.",
"",
"Open Food Facts is a collaborative project to which you can bring much more than new products: your energy, enthusiasm and ideas!",
"To discuss between contributors and make the project go forward, we use a very convenient messaging system called Slack: https://slack.openfoodfacts.org",
"",
"You can also join the Facebook group for contributors:",
"https://www.facebook.com/groups/OpenFoodFacts/",
"",
"New: we are also starting Open Beauty Facts to create a database of cosmetics: soap, toothpaste, makeup etc.",
"https://openbeautyfacts.org",
"",
"Thank you very much!",
"",
"St=C3=A9phane and the Open Food Facts team",
"https://openfoodfacts.org",
"https://twitter.com/OpenFoodFacts"
"<html><body><p>Welcome Test, your user ID: <b>tests</b></p></body></html>"
],
[
"Date: ***",
Expand All @@ -198,4 +176,4 @@
"lc: en",
"cc: world"
]
]
]

0 comments on commit c580763

Please sign in to comment.