1

I have installed a postfix server with dovecot sasl and TLS. The problem when I try to send mail from PHP code, if I use login auth type as 'smtp' the server accept the connection without any credentials. If I change it to 'login' the server check my credentials and alert if the user or password is wrong.

What is SMTP auth and how I can configure postfix to only accept auth users?

The relevant code in my main.cf file

mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
virtual_alias_domains = $mydomain
virtual_alias_maps = hash:/etc/postfix/virtual

The part of zend mail PHP code that sends mail even with a bad password or nothing.

    $options   = new SmtpOptions([
    'host' => 'mail.host.com',
    'port' => '25',
    'connection_class'  => 'smtp',
    'connection_config' => [
        'username' => 'user',
        'password' => 'bad-password',
        'ssl' => 'tls'
    ]
]);
  • 1
    You have permit_mynetworks which is probably allowing the mail to be sent without authentication. – wurtel Aug 13 '18 at 16:17
  • @wurtel Thanks you very much, that was the right direction! in the question by mistake, I dropped the problematic line: "smtpd_relay_restrictions" and removed what you offered from there. – Yehuda Hassine Aug 14 '18 at 7:35
0

The below email script is for PHP emailing with SMTP authentication. You need to use port 465 or 587 to send an email with SMTP authentication.

SMTP default port is port 25. If you are using port 25 means it will send email from the server without SMTP authentication.

Download the phpmailer from here https://github.com/PHPMailer/PHPMailer and then use the below coding.

<?php

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "name@gmail.com";                 
$mail->Password = "super_secret_password";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "name@gmail.com";
$mail->FromName = "Full Name";

$mail->addAddress("name@example.com", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

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.