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:
you are trying to send from your local machine, which probably won’t work, would have to configure everything, mainly an SMTP server
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';
Put an example with code, Programming syntax is not valid
– Sveen
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';
.– Abraão Alves