skip to Main Content

When I run the following PHP code I get an error.

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // debug
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = $uname;
$mail->Password = $pw;
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->setFrom('[email protected]', 'website registration');
$mail->addAddress($cleaned_email);
$mail->Subject = 'Please verify your account';
$msg = '[registration text...]'
$msg = wordwrap($msg, 70);
$mail->Body = $msg;
                        
if (!$mail->send()) {
  echo $mail->ErrorInfo;
  exit();
} else {
  [... add user to db, etc...]
}

The mail appears to be sent. No error is generated by PHPMailer and the database code is run.

Here is the error generated in mail.log.

Aug 22 11:47:06 server postfix/smtp[8339]: 079AB1F909: to=<outsider-at-anydomain.com>, relay=mail.brighthouse.com[47.43.26.56]:25, delay=5.7, delays=0.06/0.02/0.31/5.3, dsn=2.0.0, status=sent (250 2.0.0 <user-at-example.com> sender rejected. Please see understanding-email-error-codes for more information.)

I have tried changing the send from address to my user that I am authenticating with in the PHP code.

I have tried adding a smtpd_sender_login_maps paramter with a matching hash table to my postfix config to map the no-reply address to my user that I authenticate with, but it ignores it as an unused parameter.

Postfix config:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Raspbian)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2

# TLS parameters
smtpd_tls_cert_file = /etc/letsencrypt/live/www.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/www.example.com/privkey.pem
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions =
        permit_mynetworks
        permit_sasl_authenticated
        defer_unauth_destination
myhostname = server
mydomain = example.com
virtual_alias_domains = example2.com
virtual_alias_maps = hash:/etc/postfix/virtual
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = $mydomain
mydestination = $myhostname, $mydomain, server, localhost.localdomain, localhost
relayhost = mail.brighthouse.com
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
notify_classes = resource, software, 2bounce
home_mailbox = Maildir/
#mailbox_command =
mailbox_transport = lmtp:unix:private/dovecot-lmtp
smtpd_sender_restrictions = permit_sasl_authenticated
smtpd_recipient_restrictions =
        permit_sasl_authenticated,
        permit_mynetworks,
        reject_unauth_destination
        reject_sender_login_mismatch
smtpd_helo_required = yes
smtpd_helo_restrictions =
        permit_mynetworks,
        permit_sasl_authenticated,
        reject_invalid_helo_hostname,
        reject_non_fqdn_helo_hostname,
        reject_unknown_helo_hostname,
        check_helo_access hash:/etc/postfix/helo_access
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = noanonymous
smptd_sender_login_maps = hash:/etc/postfix/controlled_envelope_senders

2

Answers


  1. Chosen as BEST ANSWER

    My issue has nothing to do with PHP Mailer. The php code I posted works. My issue is with my email server setup. I have posted a more server related question on Super User here: https://superuser.com/questions/1580944/soho-postfix-dovecot-configuration-for-small-web-app-and-user-base

    Thank you all for the replies.


  2. The syntax for the "from" address is incorrect:

    $mail->setFrom(‘no-reply at example dot com’, ‘website registration’);

    you need to use a valid email address, such as:

    $mail->setFrom('[email protected]', 'website registration');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search