4

I'm looking for a way to submit to a mailinglist (standard ee mailing list module) when submitting a safecracker form. Basically a user will click a checkbox "yes, send me the newsletter".

Thanks!

2

I don't think there's a way to do this out of the box with Safecracker. However, you can write an extension that responds to the safecracker_submit_entry_start or safecracker_submit_entry_end hooks.

    $data = array(
            'class'     => 'Mailing_list_ext',
            'method'    => 'add_to_mailing_list',
            'hook'      => 'safecracker_submit_entry_end',
            'settings'  => '',
            'priority'  => '10',
            'version'   => $this->version,
            'enabled'   => 'y'
    );
    $this->EE->db->insert('extensions', $data);

Basically, you'll want your extension to check for the custom field you're using for the newsletter signup, then you'll have to insert your user's email address into the exp_mailing_list table:

function add_to_mailing_list($data) {
    if($this->EE->input->post('add_me_to_list') == 'yes') {
        //get the email address from the safecracker form
        $email = $this->EE->input->post('email');

        //either hardcode the list id from exp_mailing_lists or lookup with a query
        $list_id = 1;

        //generate the auth check
        $code = $this->EE->functions->random('alnum', 10);

        //insert a new row into exp_mailing_list

        $this->EE->db->query("INSERT INTO exp_mailing_list (list_id, authcode, email, ip_address) 
        VALUES ('".$this->EE->db->escape_str($list_id)."', '".$code."', 
        '".$this->EE->db->escape_str($email)."', 
        '".$this->EE->db->escape_str($this->EE->input->ip_address())."')");
    }
}

To learn more about creating EE extensions, you can view the Extension Development docs here

| improve this answer | |
  • 1
    I should note that if you're open to using another mailing list provider, there is an excellent add-on for MailChimp and EE that will allow your users to opt-in via safecracker: blog.mailchimp.com/… – Patrick Pohler Dec 11 '12 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.