• I finally got List-Unsubscribe working, but it still didn’t have the nice (expected) button at the top of Gmail. Turns out Gmail doesn’t show it for all senders:

    This only works for some senders right now. We’re actively encouraging senders to support auto-unsubscribe — we think 100% should. We won’t provide the unsubscribe option on messages from spammers: we can’t trust that they’ll actually unsubscribe you, and they might even send you more spam. So you’ll only see the unsubscribe option for senders that we’re pretty sure are not spammers and will actually honor your unsubscribe request.

    https://gmail.googleblog.com/2009/07/unsubscribing-made-easy.html

    By the time I learned that, I’d already implemented List-Unsubscribe One-Click.

    I’d appreciate if you added the code to the plugin:

    $message->headers = array();
    
    $unsubscribe_address = isset( $this->options['reply_to'] ) ?  $this->options['reply_to'] : $this->options['sender_email'];
    $message->headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
    $message->headers['List-Unsubscribe'] = '<mailto:' . $unsubscribe_address . '?subject=unsubscribe>, <' . $this->build_action_url('uc', $user, $email) . '>';

    and edit the bot check to add:

    if( isset($_POST['List-Unsubscribe']) && 'One-Click' === $_POST['List-Unsubscribe']
        || 'List-Unsubscribe=One-Click' === file_get_contents('php://input') ) {
        return true;
    }

    https://tools.ietf.org/html/rfc8058

    And I’m using your AWS SES add-on which currently DOES NOT add ANY headers.

    SesClient::sendEmail() needs to be changed to SesClient::sendRawEmail()

    In NewsletterAmazonMailer::send():

    /** @var array $ses_email */
    $ses_email = $this->prepareEmail($message);
    
    /** @var \Aws\Result $result */
    $result = $ses_client->sendRawEmail($ses_email);
    
    if( $result->get('statusCode') != 200 ) {
        $logger->error( $result );
        return false;
    }

    and NewsletterAmazonMailer::send_chunk():

    $commands[] = $ses_client->getCommand('SendRawEmail', $ses_email);

    If you want to handle errors here, the code to do so is at https://deliciousbrains.com/email-queue-ses/

    then NewsletterAmazonMailer::prepareEmail() replaced with:

    /**
     * @see https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-raw-using-sdk.html
     *
     * @param TNP_Mailer_Message $message Plain object containing message values.
     * @return array The message as an array suitable for Aws\Ses\SesClient::sendRawEmail().
     * @throws phpmailerException
     */
    function prepareEmail($message) {
    
        $newsletter = Newsletter::instance();
    
        $mail = new PHPMailer();
    
        $mail->CharSet = 'UTF-8';
    
        $sender = $message->from;
        $sendername = $message->from_name;
    
        $recipient =  $message->to;
    
        $subject = $message->subject;
    
        $htmlbody = $message->body;
        $textbody = $message->body_text;
    
        $mail->setFrom($sender, $sendername);
    
        $reply_to = $newsletter->options['reply_to'];
        if (!empty($reply_to)) {
            $mail->addReplyTo( $reply_to );
        }
    
        foreach( $message->headers as $header_name => $header_value ) {
    
            $mail->addCustomHeader(  $header_name, $header_value );
        }
    
        $mail->addAddress($recipient);
        $mail->Subject = $subject;
        $mail->Body = $htmlbody;
        if(!empty($textbody)) {
            $mail->AltBody = $textbody;
        }
    
        $mail->IsHTML(true);
    
        $mail->preSend();
    
        $message = $mail->getSentMIMEMessage();
    
        $ses_email = array(
            'RawMessage' => array(
                'Data' => $message
            )
        );
    
        return $ses_email;
    }
    • This topic was modified 4 years, 8 months ago by sarmatopia.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘List-Unsubscribe One-Click code’ is closed to new replies.