Search code examples
phpemailsendmailspam

Sending mail via PHP function


I'm trying to send simply emails via PHP mail() function from a server PLESK, using sendmail (in /usr/sbin/sendmail).

I already read the checklist by John Conde explained here PHP mail function doesn't complete sending of e-mail and my problem is most probably with the provider because my script sends emails to a gmail account, but noone else; the email sent to gmail accounts go into the SPAM folder. I'm trying to send to a [email protected] for example.

Before you ask I already checked my sender IP reputation, verified the reverse DNS result and obtained a score of 9 out 10 on http://mail-tester.com/ [spam checker].

In the php.ini of the domain I put fields as: SMTP, smtp_port, sendmail_from, sendmail_path, auth_password, auth_username. (because I have SSL certificate on the domain, I am using 465 port on a provider smtp server).

This is the snippet of code:

<?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    ini_set('display_startup_errors', 1);
    set_error_handler("var_dump");

    //
    $replyTo = "[email protected]";
    $mailTo  = "[email protected]";

    //Test sender delle Email
    $headers = 'From: '.$replyTo."\r\n".
               'Reply-To: '.$replyTo."\r\n" .'X-Mailer: PHP/' . phpversion();

    if (mail($mailTo,"Title","Body of the message",$headers)){
       echo "Mail ok";
    } else {
       echo "Error Mail";
    }
?>

This is the log of the server under mail directory

Aug 23 10:38:20 webserver check-quota[1905]: Starting the check-quota filter...
Aug 23 10:38:20 webserver journal: plesk sendmail[1904]: handlers_stderr: SKIP
Aug 23 10:38:20 webserver journal: plesk sendmail[1904]: SKIP during call 'check-quota' handler
Aug 23 10:38:20 webserver postfix/pickup[1560]: DAC521A8B: uid=10003 from=<[email protected]>
Aug 23 10:38:20 webserver postfix/cleanup[1910]: DAC521A8B: message-id=<[email protected]>
Aug 23 10:38:20 webserver postfix/qmgr[1561]: DAC521A8B: from=<[email protected]>, size=450, nrcpt=1 (queue active)
Aug 23 10:38:20 webserver postfix-local[1915]: postfix-local: [email protected], [email protected], dirname=/var/qmail/mailnames
Aug 23 10:38:20 webserver postfix-local[1915]: cannot chdir to mailname dir fcaliari: No such file or directory
Aug 23 10:38:20 webserver postfix-local[1915]: Unknown user: [email protected]
Aug 23 10:38:20 webserver postfix/pipe[1914]: DAC521A8B: to=<[email protected]>, relay=plesk_virtual, delay=0.05, delays=0.03/0.01/0/0.01, dsn=2.0.0, status=sent (delivered via plesk_virtual service)
Aug 23 10:38:20 webserver postfix/qmgr[1561]: DAC521A8B: removed

In the logs appears this email [email protected] which I never used in the php.ini file or in the script

Any help appreciate!


Solution

  • When sending mail within a php script I would highly recommend using an SMTP relay to send the email via a dedicated email server instead of sending directly through your web server. Reason for this is: If you are hosted on a shared server, there is a chance that other people who are also hosted on that server could be sending spam (which will cause your emails to be marked as spam).

    I have had a good experience with using PHPMailer, there is lots of settings which you can tweak to make the setup work with your environment/setup.

    For reference, please see: https://github.com/PHPMailer/PHPMailer