How to send PHP emails using port 587?

Asked

Viewed 894 times

0

I need to do an integration with the database, where the client makes the payment and the system takes this information as (client’s e-mail), transaction status (approved), (transaction date) (approval date) and registered in the database. Then send 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 automatically generated login and password 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 doesn’t work and Gmail and other providers reject the messages.

The syntax of the programming would be the following:

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. 
  • Put an example with code, Programming syntax is not valid

  • To make it work, make sure the host is $mail->Host = 'smtp.gmail.com'; because it is standard Gmail. Enables TLS encryption $mail->SMTPSecure = 'tls';.

1 answer

1

The function mail has to be configured in the back end of your server and your server has to have an SMTP email service, on Linux servers probably use the sendmail, but I’m going to the point where it goes because I think the question is not the door but the type of shipment.

Two possibilities:

  1. you are trying to send from your local machine, which probably won’t work, would have to configure everything, mainly an SMTP server

  2. you are sent from your server that already has email services (SMTP, PO3 and IMAP), however using the function mail() he will send via sendmail, what does not pass for the "normal authentication", even if it was in the door 587 or any other port, that may not arrive in the INBOX of the recipient, that is because the very means that the function mail() use are usually barred, function mail() for many times not having daily limit control of sending, size and checking usually end up being categorized by recipients as SPAM and that often not even in the SPAM Box will arrive, because it may have been blocked before that, may have been blocked by:

    • At the exit of SMTP, because your own hosting has some kind of Spamming policy

    • In the attempt of sendmail with SMTP if "communicate" with the target server, which can be rejected right there.

    I am not saying that you are practicing SPAM, I am saying that policies against SPAM have no way to identify whether or not you are a SPAMMER, as this sending method has no possible way of checking guaranteed.

So how to resolve sending emails

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

As I explained in this reply /a/261314/3635

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 8 first items you make yourself, that this is very broad and impossible to answer here, so we come to the ones that interest us it is the:

**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, the important thing is to send to the customer and Adm, so it should look like this, after you’ve done all 8 items 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 a day (each recipient I believe counts as a different message).

Just for the record, if it’s without TLS and SSL, or it’s TLS as well, maybe the port is 587, but that’s because it’s your default server/hosting, you should consult with them, so if it’s with TLS, maybe it should stay that way:

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

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

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

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

If it is Gmail or Outlook (old)

For gmail the setting should be 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 believe it is necessary to enable Gmail to allow sending, on the link: https://myaccount.google.com/intro/security

In outlook I don’t remember for sure, maybe it is still smtp.live.com, which would look like:

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

Or maybe it’s now by office365 (I don’t use this, correct me if I’m wrong about something):

$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
  • Guilherme Nascimento, sensational your explanation. I would like to know how much you would charge me to create this syntax that I passed on the question because I’m kind of a layman in PHP so I see that you have very advanced knowledge. I am waiting for a budget if you can give me your email "without the arroba" to avoid spammers I am grateful.

  • @RodrigoPorto [email protected]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.