PHP mail() function & PHMailer doubts

340 views Asked by At

I have few doubts regarding the mail options in PHP.

  1. Whatever I have read, I can not send mail from localhost without SMTP. Is it correct ?

  2. For sending the mail from localhost using the PHP mail() function, I need to make few changes in these two files. one is 'php.ini' & another one is 'sendmail.ini'. changes like setting smtp_server, port, username & password. This option is working for me perfectly & I have sent mail using this. Just want to confirm that am I following the correct way for sending the mail from localhost ?

  3. If I just have to send simple mails, is there any much difference between mail() function & PHPMailer ?

  4. After changing the two ini files(php.ini & sendmail.ini), I am using the example given on 'https://github.com/PHPMailer/PHPMailer' at the end of the page, but that example is not working. I am using the same SMTP in the PHPMailer example which I have used in 'sendmail.ini', but still that is not working. But If I comment out the SMTP part in PHPMailer example, which is

    /*$mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '[email protected]'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; //Enable TLS encryption,sslalso accepted $mail->Port = 587; */

then it is working perfectly fine. I do not know why do I have to comment out the SMTP part ?

Please help me to clear my doubts. Thanks in advance

2

There are 2 answers

1
Peter On BEST ANSWER

Whatever I have read, I can not send mail from localhost without SMTP. Is it correct ?

You need to have mail server (like linux sendmail) to send messages. You can use your server's built-in mail post server or connect to SMTP. PHP has no built-in mail server / "postman"

For sending the mail from localhost using the PHP mail() function, I need to make few changes in these two files. one is 'php.ini' & another one is 'sendmail.ini'. changes like setting smtp_server, port, username & password. This option is working for me perfectly & I have sent mail using this. Just want to confirm that am I following the correct way for sending the mail from localhost ?

It's OK as far as you setup your hostname/domain/dns records properly otherwise your mail will land into junk/spam box. Read about SPF record for more info.

If I just have to send simple mails, is there any much difference between mail() function & PHPMailer ?

PHPMailer by default uses mail(). PHPMailer is just nice and easy interface/layer for mail() so you don't have to write complicated mail headers by yourself. In case of STMP phpmailer uses sockets

After changing the two ini files(php.ini & sendmail.ini), I am using the example given on 'https://github.com/PHPMailer/PHPMailer' at the end of the page, but that example is not working. I am using the same SMTP in the PHPMailer example which I have used in 'sendmail.ini', but still that is not working. But If I comment out the SMTP part in PHPMailer example, which is

check what echo 'Mailer Error: ' . $mail->ErrorInfo; says

1
René Höhle On

So what you've written before is correct if you want to send E-Mails from your local Machine you need an MTA (Mail Transfer Agent).

You can install a Postfix if you use Linux or for Windows there are good tools to make a local mailserver which catch all your local mails. To configure a local Postfix to catch all incoming mails is a bit more complicated.

https://www.hmailserver.com/

The default mail function from PHP use sendmail to send your emails to the world. When you have libraries like PHPMailer or what i prefer SwiftMailer. Then you have a lot of features like the sending over SMTP.

Normally sendmail redirect your E-Mails to an existing MTA. Sendmail is a relict from the early days and the most programs are sendmail compatible.

And your last questions. If you comment out the line $mail->isSMTP();then you send your mails over a local relay (sendmail) so its possible that you haven't installed openssl? and you can't connect over TLS to your smtp server. You should check if the port 587 is open and available.

To get better Error information you can use $mail->ErrorInfo; to see what happen.