N
N
Nikita Kornilov2018-11-11 00:37:18
PHP
Nikita Kornilov, 2018-11-11 00:37:18

How to send mail from PHP?

Good day! I am now implementing a simple HTML form on the site that collects data and sends it by letter to a specific address. Wrote the following code:

$to = '[email protected]';
$subject = 'Тема письма';
$from = ' [email protected]>';

$headers = "From: $from\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "Return-Path: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP" . phpversion() . "\r\n";

$message =
  "<html>
    <body>Текст письма</body>
  </html>";

mail($to, $subject, $message, $headers);

The letter was sent and received. Everything was fine except that it constantly ended up in the spam folder. To fix this, I created a mail domain at the hoster, a mailbox like [email protected] and activated all the features that reduced the likelihood of getting into spam (SpamAssassin, DKIM, DMARC and SSL). After that, I checked the letter in mail-tester , which gave 1.6 points and showed that the Return-Path header contains an address that does not belong to this form at all. As it turned out later, it was taken from the php.ini file . I changed the sendmail_path parameter in it to /usr/sbin/sendmail -t -i -f [email protected] and everything worked as it should. Letters are no longer going to spam, great.
But this immediately raises a couple of questions:
1. What if another site with a different domain is located on the same server and you also need to send a letter from it?
2. I read that there is a special PHPMailer library that can send emails through third-party SMTP servers. What is it at all and is it worth using it for this?
3. And which of these two methods is better to use for simply sending emails from the site? Are external SMTP servers somehow fundamentally better or worse than the standard mail() function ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Sapegin, 2018-11-11
@Nikkorfed

I fought with mail() for a long time and eventually switched to PHPMailer+SMTP. It works more stable. You can use Yandex SMTP if Yandex mail service for domain is used for the domain from which the message is being sent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question