Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeremias 53 posts 278 karma points
    Oct 27, 2017 @ 12:58
    Jeremias
    0

    Add a file as attachment to the e-mail

    Hello @all,

    I need to add an attachment to the newsletter. I already use a render-task to replace first- and lastname...

    I also use the function "Insert dynamic content from URL" to create the content from the newsletter.

    My question is: Is there a way to add a MediaPicker on the page in the content-area and add it as attachment to the newsletter? Or has any body another idea?

    Thank you for your help!

  • Markus Johansson 1902 posts 5706 karma points MVP c-trib
    Oct 27, 2017 @ 13:31
    Markus Johansson
    0

    Hi!

    If you are sending from the content area this should be possible if you create a render task that gets the URL to the file and then attach the file to the MailMessage-class.

    I don't have any code examples but on the newsletter-object there is a property that contains the nodeid (if it is a node) this could be used and then you could get the IPublishedContent-item to figure out the medie file url.

    // m

  • Markus Johansson 1902 posts 5706 karma points MVP c-trib
    Oct 29, 2017 @ 13:37
    Markus Johansson
    1

    Hi again Jeremias!

    This is an example of how this can be done: https://gist.github.com/enkelmedia/e3d9ff59d38bb36575d7af19d6ad61c1

    Details about render tasks and how to configure them can be found here: http://support.newsletterstudio.org/customer/en/portal/articles/1052872-control-content-with-render-tasks

    Hope that this helps to solve your problems!

  • Jeremias 53 posts 278 karma points
    Oct 30, 2017 @ 08:29
    Jeremias
    0

    Hi Markus,

    thank you for your code-snippet. That's great! Thank you!

    But now I have the problem, that parameters.Newsletter.ContentNodeId is NULL... enter image description here

    Here the pictures from the Content-Node and from the Newsletter: enter image description here enter image description here

    Is there one more thing to do, that the ContentNodeId gets saved in the parameters?

  • Markus Johansson 1902 posts 5706 karma points MVP c-trib
    Oct 30, 2017 @ 08:31
    Markus Johansson
    100

    Hi!

    Did you press "save and publish" before you tried to send? I think that you need to do this before the content get updated in the newsletter node.

    Edit: This will only work if you are sending from the content-section, in other words if the newsletter is created from a umbraco node.

    Edit2: It looks like you are trying to render the node using the "Insert content url"-feature? That would not work with this solution - if you need that solution one option would be to replace the default rendertask for "InsertUrl" with a custom one that inserts the attachment.

    This is the code for the default "RenderUrlRenderTask.cs", if you use this as your starting-point, get the node based on the URL and then get that IPublishedContent and look if there is an attachment.

    https://gist.github.com/enkelmedia/79ea9a81d09d2fb4f74206584a834a7d

    I would say that this is a quite "hacky" solution, I would go for the "Send out"-data type in the content-section:

    http://support.newsletterstudio.org/customer/en/portal/articles/1053067-sending-from-the-content-section

    // m

  • Jeremias 53 posts 278 karma points
    Oct 30, 2017 @ 09:34
    Jeremias
    0

    Hi!

    Thank you!

    This will only work if you are sending from the content-section, in other words if the newsletter is created from a umbraco node.

    That war my problem. I added the Newsletter Sendout to the Documenttype and it works :)

    Thank you for helping!

  • Markus Johansson 1902 posts 5706 karma points MVP c-trib
    Oct 30, 2017 @ 09:47
    Markus Johansson
    0

    Cool!

    The thing is that when you send from the content section, the newsletter will be connected to the node that is used to create it. When you are using the "insert url content"-feature there is no real connection except for the URL that is entered - this could even be an external URL on another website.

    Great that you got it working =D

    Please let me know if you have any other questions etc.

    // m

  • Jeremias 53 posts 278 karma points
    Nov 08, 2017 @ 13:46
    Jeremias
    0

    Hi,

    now I have one more question to this: Is it also possible to attach the file to the test-email? Now it is only attached in the 'real' sending...

  • Markus Johansson 1902 posts 5706 karma points MVP c-trib
    Nov 08, 2017 @ 14:51
    Markus Johansson
    0

    Hi!

    No, not at the moment as we're not passing the MailMessage-object to the pre-render methods.

    My tip would be to send a "real" message to a list of test-subscibers (like your self and some collegues or something).

    // m

  • Jeremias 53 posts 278 karma points
    Nov 08, 2017 @ 15:04
    Jeremias
    0

    Ok. No problem. Thank you!

  • keilo 568 posts 1023 karma points
    Nov 08, 2017 @ 14:26
    keilo
    0

    Hi Jeremias

    I am working on similar use case to add attachment to the newsletter.

    Can you share the RenderTask code snippet that you manage to get it working? I have seen the two different snippet above and wondering how you get on with creating add attachment rendertask.

  • Jeremias 53 posts 278 karma points
    Nov 08, 2017 @ 14:46
    Jeremias
    1

    Hi Keilo,

    of course! Here is my RenderTask to attach the file:

    public class RenderNewsletterMarker : RenderTask {
    private IMemberService ms;
    string _attachmentPath = String.Empty;
    
    public RenderNewsletterMarker()
    {
        ms = ApplicationContext.Current.Services.MemberService;
    }
    
    public override void ProcessPreRender(RenderResult renderResult, RenderTaskParameters parameters)
    {
        try
        {
            if (parameters.Newsletter.ContentNodeId.HasValue && parameters.Newsletter.ContentNodeId.Value > 0)
            {
                // Get this node as a IPublishedContent-object
                var helper = new UmbracoHelper(UmbracoContext.Current);
                var node = helper.TypedContent(parameters.Newsletter.ContentNodeId);
    
                if (node.HasProperty("nl_attachment") || node.GetPropertyValue("nl_attachment") != null)
                {
                    var attachment = node.GetPropertyValue<IPublishedContent>("nl_attachment");
    
                    if (attachment.GetType().Name == "Image")
                    {
                        var _attachment = (Image)attachment;
                        _attachmentPath = HttpContext.Current.Server.MapPath(_attachment.Url);
                    }
                    else
                    {
                        var _attachment = new KomXCms.Models.Generated.File(attachment);
                        _attachmentPath = HttpContext.Current.Server.MapPath(_attachment.Url?.ToString());
                    }
                }
            }
        }
        catch (Exception e)
        {
            LogHelper.Info(this.GetType(), e.Message);
        }
    }
    
    public override void ProcessPreview(RenderResult renderResult, RenderTaskParameters parameters)
    {
        //renderResult.MessageBody = renderResult.MessageBody.Replace("[anrede]", "ProcessPreview");
    }
    
    public override void ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters)
    {
        if (!string.IsNullOrEmpty(_attachmentPath))
        {
            // add new attachment to the e-mail;
            parameters.MailMessage.Attachments.Add(new Attachment(_attachmentPath));
        }
    } }
    

    I use the newsletterstudio like this: I have a content-Node which is rendering the whole content for the Newsletter: enter image description here

    On the same page I have the sendout: enter image description here This is necessary, that the the RenderTask knows from wich page he should take the Attachment.

    I hope this helps you!

  • Jeremias 53 posts 278 karma points
    Nov 08, 2017 @ 15:17
    Jeremias
    0

    it's also helpful to watch the last two videos on this page:

    https://www.newsletterstudio.org/videos/

    (Send newsletters from the content section, Work with the content using Render Tasks)

  • keilo 568 posts 1023 karma points
    Nov 08, 2017 @ 15:38
    keilo
    0

    That looks cool Jeremias!

    Many thanks for sharing your use case and rendertask !

    I was using the Newsletter standalone section and wasnt quite sure on how to operate the RenderTask only when its used in Content Section. Looking for ContentId and perhaps a flag within RenderTask should not disturb Newsletter Section standalone usage along with occasional Content section usage.

Please Sign in or register to post replies

Write your reply to:

Draft