PHP mail: how to override or bypass mail.log setting

后端 未结 4 1243
醉酒成梦
醉酒成梦 2020-12-21 12:35

phpinfo() returns this particular setting which is problematic:

mail.log    /var/log/phpmail.log

The problem is I can\'t access the phpmail

相关标签:
4条回答
  • 2020-12-21 13:08

    I'm a newbie in PHP so I don't know if my solution is really a way to solve this issue or if it works due to a coincidence!

    I have the same problem presented here: a call to the PHP function mail() produces a PHP warning. It's all explained in the first message. I also don't possess the permission to edit the mail.log or the directory in which to save it.

    Googling, I found a code that call the mail() preceded with a @. I tried to use the code @mail() and it worked!

    So this is the working code that I use:

    $mail_to = "MyAddress@email.com";
    $mail_from = $email; // This e-mail address is taken by an HTML form
    $mail_subject = "Test PHP function";
    $mail_body = "<p>This is the body message for the test of a PHP function!</p>";
    
    // HTML headers
    $mail_in_html = "MIME-Version: 1.0
    ";
    $mail_in_html .= "Content-type: text/html; charset=iso-8859-1
    ";
    $mail_in_html .= "From: <$mail_from>";
    
    // Submission process
    if(@mail($mail_to, $mail_subject, $mail_body, $mail_in_html))
    {
        print "E-mail sent!";
    }
    else
    {
        print "Error: e-mail not sent.";
    }
    
    0 讨论(0)
  • 2020-12-21 13:13

    I would first try to contact whoever is managing your server, he or she should fix this problem.

    If that doesn't work, the mail.log setting is a PHP_INI_PERDIR setting, meaning that:

    Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3)

    Obviously, you can't access the system's php.ini & httpd.conf, but if a .user.ini is provided to you, or you're allowed to change settings in .htaccess, then that would be a way to solve this.

    See:
    .user.ini files
    Set php.ini Values Using .htaccess

    Alternatively, you could consider using Swift Mailer or PHPMailer, which don't need the mail() function. In general, using mail() is discouraged, it's very basic and fairly difficult to use correctly & securely.

    0 讨论(0)
  • 2020-12-21 13:13

    I ended up finding this code and it's good enough:

    error_reporting(0);
    

    Inserted right before calling the mail() function. Error_log no longer filling up.

    0 讨论(0)
  • 2020-12-21 13:28

    You can change the mail.log file path in php.ini setting to a writable location for Apache or any server you are using.

    0 讨论(0)
提交回复
热议问题