Open Closed

send email when admin register new user #1848


User avatar
0
shobhit created
  • ABP Framework version: v4.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace: NA
  • Steps to reproduce the issue:" NA

Query: We need provision to send an email to user when tenant/host admin register any new user. Please confirm the process.


6 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi,

    You can start by creating a class in the MyProjectName.Application project. Here is my file structure of the AppService I created:

    Then you can customize it as follows 👇👇

        [Dependency(ReplaceServices = true)]
        [ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService), typeof(CustomIdentityUserAppService))]
        public class CustomIdentityUserAppService : IdentityUserAppService
        {
            public CustomIdentityUserAppService(IdentityUserManager userManager, IIdentityUserRepository userRepository, IIdentityRoleRepository roleRepository, IOrganizationUnitRepository organizationUnitRepository, IIdentityClaimTypeRepository identityClaimTypeRepository, IdentityProTwoFactorManager identityProTwoFactorManager, IOptions<IdentityOptions> identityOptions, IDistributedEventBus distributedEventBus) 
                : base(userManager, userRepository, roleRepository, organizationUnitRepository, identityClaimTypeRepository, identityProTwoFactorManager, identityOptions, distributedEventBus)
            {
            }
    
            public override async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
            {
                var identityUserDto = await base.CreateAsync(input);
    
                // ...
                Console.WriteLine("Sending email...");
                
                return identityUserDto;
            }
        }
    

    For more information you can check this document.

  • User Avatar
    0
    shobhit created

    Thanks berkansasmaz. Consider my limited understanding about ABP framework, i have 2 points:

    1- As per my understanding ABP support email template creation. if true assume user has created a template. 2- ABP we can setup smtp server and assume that smtp is configured

    Based on these 2 assumption can you please give me sample code to send the email to user with filled tempalte email.

  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi,

    Replace the CustomIdentityUserAppService I shared in my previous answer with the following code:

        [Dependency(ReplaceServices = true)]
        [ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService), typeof(CustomIdentityUserAppService))]
        public class CustomIdentityUserAppService : IdentityUserAppService
        {
            private const string ToEmail = "info@abp.io";
    
            private readonly IEmailSender _emailSender;
            private readonly ITemplateRenderer _templateRenderer;
    
            public CustomIdentityUserAppService(IdentityUserManager userManager, IIdentityUserRepository userRepository, IIdentityRoleRepository roleRepository, IOrganizationUnitRepository organizationUnitRepository, IIdentityClaimTypeRepository identityClaimTypeRepository, IdentityProTwoFactorManager identityProTwoFactorManager, IOptions<IdentityOptions> identityOptions, IDistributedEventBus distributedEventBus, ITemplateRenderer templateRenderer, IEmailSender emailSender) 
                : base(userManager, userRepository, roleRepository, organizationUnitRepository, identityClaimTypeRepository, identityProTwoFactorManager, identityOptions, distributedEventBus)
            {
                _templateRenderer = templateRenderer;
                _emailSender = emailSender;
            }
    
            public override async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
            {
                var identityUserDto = await base.CreateAsync(input);
                
                var emailBody = await _templateRenderer.RenderAsync("Hello", new HelloModel
                {
                    Name = "John"
                });
                
                await _emailSender.SendAsync(
                    ToEmail,
                    "ABP Commercial",
                    emailBody
                );
                
                return identityUserDto;
            }
        }
    

    Then create a folder named Emailing and create the same structure as Emailing folder in the picture below:

    Hello.tpl

        <!DOCTYPE html>
        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta charset="utf-8" />
        </head>
        <body>
            hi, {{model.name}}
        </body>
        </html>
    

    HelloModel.cs

        public class HelloModel
        {
            public string Name { get; set; }
        }
    

    MyTemplateDefinitionProvider.cs

        public class MyTemplateDefinitionProvider : TemplateDefinitionProvider
        {
            public override void Define(ITemplateDefinitionContext context)
            {
                context.Add(
                    new TemplateDefinition("Hello") //template name: "Hello"
                        .WithVirtualFilePath(
                            "/Emailing/Templates/Hello.tpl", //template content path
                            isInlineLocalized: true
                        )
                );
            }
        }
    

    Then add the following code in MyProjectName.Application.csproj:

      <ItemGroup>
        <EmbeddedResource Include="Emailing\Templates\*.tpl" />
      </ItemGroup>
    

    Finally, add the following code inside the ConfigureServices method in MyProjectNameApplicationModule:

                Configure<AbpVirtualFileSystemOptions>(options =>
                {
                    options.FileSets.AddEmbedded<MyProjectNameApplicationModule>("MyProjectName.Emailing");
                });
    

    When you add a user, you can find the outgoing mail in the Logs/logs.txt file (at the bottom of the MyProjectName.HttpApi.Host project).

    For more information, you can refer to the following documents and demo application:

    • https://community.abp.io/articles/replacing-email-templates-and-sending-emails-jkeb8zzh

    • https://docs.abp.io/en/abp/latest/Text-Templating#replacing-the-existing-templates

    • https://docs.abp.io/en/abp/latest/Emailing

    • https://commercial.abp.io/modules/Volo.TextTemplateManagement

    • https://github.com/abpframework/abp-samples/tree/master/TextTemplateDemo

  • User Avatar
    0
    shobhit created

    Hello berkansasmaz , Thanks a lot for sample. Please find the attached screen shot. I am getting the "assembly reference missing issue". what i am missing.

  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    This may be because the version of my application is different from the version of your application.

    Could you please delete and recreate the your constructor?

    PS: Don't forget to inject IEmailSender and ITemplateRenderer when you recreate constructor.

    If the problem is solved, just close the question 🙂

    Happy coding 💻

  • User Avatar
    0
    shobhit created

    Thanks for help

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11