chillywilly
chillywilly

Reputation: 434

replyto email is sometimes ignored by Apple Mail

My client uses Apple Mail on his iPhone (iPhone 8, iOS 13.3) to respond to incoming website emails where we have a PHPMailer contact form. About half the time when he replies, the "replyto" email address is ignored by Mail and the "from" address is used instead. Since we're using the postmaster address in the "from" to improve email deliverability, he ends up replying to Postmaster instead of the customer. I use Gmail, and I've never experienced this problem with my own forms using the same type of code so this seems like an issue with Apple Mail. Nevertheless, I thought I'd ask if there was something I could do to solve this. Maybe there's something wrong with my code?

validation:

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contact'])) {

  // validation code where $email and $name are defined as submitted and $error is true if validation fails

  if ($error === false) {

    $email_package = new Email();

    // assemble HTML message to be emailed
    $email_html = "<html email>";

    $email_package->addReplyTo($email, $name);
    $email_package->addRecipient('[email protected]', 'Customer Service');
    $email_package->setSubject('Message from Customer');
    $email_package->isHTML(true);
    $email_package->setBodyHTML($email_html);
    $email_package->send();

    if($email_package->mailSuccess === false) {
      error_log("FAIL: " . $email_package->mailError);
    }
  }
}

email script:

date_default_timezone_set('America/Los_Angeles');

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

class Email {
  private $mailer;

  var $mailSuccess = false;
  var $mailError = '';

  function __construct() {
    $this->mailer = new PHPMailer(true);
    $this->mailer->isSMTP();
    $this->mailer->Host = 'mail.example.com';
    $this->mailer->SMTPAuth = true;
    $this->mailer->Username = '[email protected]';
    $this->mailer->Password = 'password';
    $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $this->mailer->Port = 465;
    $this->mailer->CharSet = PHPMailer::CHARSET_UTF8;
    $this->mailer->DKIM_domain = $domain;
    $this->mailer->DKIM_selector = 'selector';
    $this->mailer->DKIM_identity = '[email protected]';
    $this->mailer->DKIM_private = 'private.key';
    $this->mailer->setFrom('[email protected]', 'Postmaster');
  }

  public function addReplyTo($address, $name = '') {
    return $this->mailer->addReplyTo($address, $name);
  }

  public function addRecipient($address, $name = '') {
    return $this->mailer->addAddress($address, $name);
  }

  public function setSubject($subject) {
    $this->mailer->Subject = $subject;
  }

  public function isHTML($bool) {
    $this->mailer->isHTML($bool);
  }

  public function setBodyHTML($email_html) {
    $this->mailer->Body = $email_html;
  }

  public function setBodyText($email_text) {
    $this->mailer->AltBody = $email_text;
  }

  public function send() {
    try {
      $this->mailer->send();
      $this->mailSuccess = true;
    } catch (Exception $e) {
      $this->mailError = $e->errorMessage();
      error_log('PHPMailer Error: ' . $this->mailError);
    } catch (\Exception $e) {
      $this->mailError = $e->getMessage();
      error_log('PHPMailer Error: ' . $this->mailError);
    }
  }
}

Anonymized email headers with SMTPDebug = 2:

2022-08-09 05:08:51 SERVER -> CLIENT: 220-host1234.webhost.com ESMTP Exim 4.95 #2 Tue, 09 Aug 2022 00:08:51 -0500 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2022-08-09 05:08:51 CLIENT -> SERVER: EHLO example.com
2022-08-09 05:08:51 SERVER -> CLIENT: 250-host1234.webhost.com Hello example.com [IP.AD.DRE.SS]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250 HELP
2022-08-09 05:08:51 CLIENT -> SERVER: AUTH LOGIN
2022-08-09 05:08:51 SERVER -> CLIENT: 334 fifoahfoahskjs
2022-08-09 05:08:51 CLIENT -> SERVER: [credentials hidden]
2022-08-09 05:08:51 SERVER -> CLIENT: 334 gehjfakshfgsaj
2022-08-09 05:08:51 CLIENT -> SERVER: [credentials hidden]
2022-08-09 05:08:51 SERVER -> CLIENT: 235 Authentication succeeded
2022-08-09 05:08:51 CLIENT -> SERVER: MAIL FROM:<[email protected]>
2022-08-09 05:08:51 SERVER -> CLIENT: 250 OK
2022-08-09 05:08:51 CLIENT -> SERVER: RCPT TO:<[email protected]>
2022-08-09 05:08:51 SERVER -> CLIENT: 250 Accepted
2022-08-09 05:08:51 CLIENT -> SERVER: DATA
2022-08-09 05:08:51 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself
2022-08-09 05:08:51 CLIENT -> SERVER: Date: Mon, 8 Aug 2022 22:08:51 -0700
2022-08-09 05:08:51 CLIENT -> SERVER: To: Customer Service <[email protected]>
2022-08-09 05:08:51 CLIENT -> SERVER: From: Postmaster <[email protected]>
2022-08-09 05:08:51 CLIENT -> SERVER: Reply-To: Customer <[email protected]>
2022-08-09 05:08:51 CLIENT -> SERVER: Subject: Message from Customer
2022-08-09 05:08:51 CLIENT -> SERVER: Message-ID: <[email protected]>
2022-08-09 05:08:51 CLIENT -> SERVER: X-Mailer: PHPMailer 6.6.3 (https://github.com/PHPMailer/PHPMailer)
2022-08-09 05:08:51 CLIENT -> SERVER: MIME-Version: 1.0
2022-08-09 05:08:51 CLIENT -> SERVER: Content-Type: text/html; charset=utf-8
2022-08-09 05:08:51 CLIENT -> SERVER:
2022-08-09 05:08:51 CLIENT -> SERVER: <html><body><p>This is a test with Debug set to 2.</p><h4 style='margin: 2em 0 0; border-left: 1px solid black; padding-left: 1em;'>Chilly Willy<br>[email protected]</h4></body></html>
2022-08-09 05:08:51 CLIENT -> SERVER:
2022-08-09 05:08:51 CLIENT -> SERVER: .
2022-08-09 05:08:51 SERVER -> CLIENT: 250 OK id=1oLHU3-002bvb-Vb
2022-08-09 05:08:51 CLIENT -> SERVER: QUIT
2022-08-09 05:08:52 SERVER -> CLIENT: 221 host1234.webhost.com closing connection

Upvotes: 0

Views: 178

Answers (0)

Related Questions