How to send emails with PHP using port 587?

0

I need to do a database integration, where the customer makes the payment and the system takes this information as (customer email), transaction status (approved), (date of transaction) (date of approval ) and registers in the database. It then sends an automatically generated login and password to the client's email using PHP's "mail ()" function.

How do I have PHP register the client in the database and then send the login and password generated automatically to this client using port 587 of the VPS server?

Port 25 is blocked in Brazil, so the "mail ()" function that uses port 25 simply does not work and Gmail and other providers reject the messages.

The syntax of the programming would be as follows:

Se o pagamento aprovado: 

{ 
Cadastra e-mail do cliente no campo e-mail, 
Transforma este e-mail em senha, 
Cadastra senha no DB na tabela users, password

Altera status do cliente para "ativo" 
Cadastra data da transação
Cadastra data da aprovação
Cadastra/Altera data do próximo Vencimento

Verifica se o cliente já existe no banco de dados
Envia login e senha para o e-mail do cliente usando a porta 587 do servidor. 
Envia cópia deste e-mail para o administrador do sistema. 
    
asked by anonymous 09.06.2018 / 12:22

1 answer

1

The mail function has to be configured on the back end of your server and your server must have an SMTP mail service, on Linux servers you probably use sendmail , but I'm going to I think the question is not the door but the type of sending.

Two possibilities:

  • You are trying to send from your local machine, which probably will not work, would have to configure everything, especially an SMTP server

  • You are sent from your server that already has email services (SMTP, PO3 and IMAP), however using the mail() function it will send via sendmail , which does not go through "normal authentication", even that is on port 587 or any other port, this may not arrive in the recipient's INBOX, because the very media that the mail() function uses are usually barred, the mail() function often does not have daily limit control of sending, size and checking usually end up being categorized by the recipients as SPAM and often not even in the SPAM Box will arrive, this may have been blocked before that, it may have been blocked by:

    • In the SMTP output, because your own hosting has some kind of SPAM policy

    I'm not saying that you're practicing SPAM , I'm saying that SPAM policies have no way of identifying whether or not you're a SPAMMER, since this method of sending does not have any guaranteed way of checking .

  • Then how to solve the sending of emails

    Simple but not so much, sending via SMTP authenticated by a real email account and preferably by a port with security certificates (if you have it, it is possible), of course it is not 100% guaranteed, but it will work much better certainly compared to sendmail , in the case of PHP to communicate with an SMTP server, be it outlook.com, or gmail, or yahoo, you can use the:

    As I explained in this response link

    So following this part:

    Cadastra e-mail do cliente no campo e-mail, 
    Transforma este e-mail em senha, 
    Cadastra senha no DB na tabela users, password
    
    Altera status do cliente para "ativo" 
    Cadastra data da transação
    Cadastra data da aprovação
    Cadastra/Altera data do próximo Vencimento
    
    Verifica se o cliente já existe no banco de dados
    Envia login e senha para o e-mail do cliente usando a porta 587 do servidor. 
    Envia cópia deste e-mail para o administrador do sistema. 
    

    All first 8 items yourself do, that this is very broad and impossible to answer here, so we come to the ones that interest us that are the ones:

    **Envia login e senha para o e-mail do cliente** usando a porta 587 do servidor. 
    **Envia cópia deste e-mail para o administrador do sistema.** 
    

    Forget the port now, what matters is to send the client and adm, so it should be like this, after all 8 items have been made in phpmailer do this:

    $mail = new PHPMailer(true);
    try {
        $mail->SMTPDebug = 2;                                 // desative isto depois de efetuar os testes
        $mail->isSMTP();
        $mail->Host = 'smtp.empresax.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'senha';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;
    
        //Email que o cliente deve ver como remetente
        $mail->setFrom('[email protected]', 'Mailer');
    
        //Envia uma cópia oculta para o adm
        $mail->addBCC('[email protected]');
    
        //Content
        $mail->isHTML(true);
        $mail->Subject = 'Sua senha esta pronta';
        $mail->Body    = 'Conteudo HTML da mensagem';
    
        $mail->send();
        echo 'Mensagem enviada com sucesso';
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    

    Of course this depends on your server settings, and it is likely that your server and anti-SPAM policies will prevent you from sending more than 100 messages per day (each recipient I believe counts as a different message).

    Just to note, if it is without TLS and SSL, or it is TLS too, maybe the port is 587, but this is because it is your server / hosting standard, you should check with them, so if it is with TLS, maybe should look like this:

        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
    

    If it is without TLS and without SSL, then you have to do this:

    $mail->SMTPSecure = false;
    $mail->SMTPAutoTLS = false;
    $mail->Port = 587;
    

    But as I said, look at the documentation for your hosting (usually it has) or consult technical support to know which ports to use, it's usually the same configuration that your client uses in Outlook or Mozilla Thunder Bird.

    If it's Gmail or Outlook (old hotmail)

    For gmail the configuration should look something like:

        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'senha';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
    

    However, I think it is necessary to enable Gmail to allow the submission, at the following link: link

    In outlook I can not remember for sure, maybe it's still smtp.live.com, which would look like:

    $mail->Host = 'smtp.office365.com';
    $mail->Port       = 587;
    $mail->SMTPSecure = 'tls';
    

    Or maybe it's now from office365 (I do not use this, please correct me if you've been wrong about something):

    $mail->Host = 'smtp.office365.com';
    $mail->Port       = 587;
    $mail->SMTPSecure = 'tls';
    
        
    09.06.2018 / 22:05