Strange php mail() problem

Discussion in 'Programming/Scripts' started by Wilt, Jul 19, 2018.

Tags:
  1. Wilt

    Wilt Member HowtoForge Supporter

    Hi,
    I have got one of those strange problems that has be sticking pins in my eyes! I am sending emails from a php page using the php mail() function and the emails need to be in HTML format.
    This bit of code works perfectly but (as expected) sends the email in text format. Note the second $headers line is commented out.
    PHP:
    $headers "MIME-Version: 1.0\r\n";
    //$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: " $sender;

    // send the message with mail()
    if(mail($recipient$message_subject$emailmessage$headers)){

            
    $message $message "We have sent you an email to confirm your address. Please follow the instructions in the email to complete your membership."
            echo 
    json_encode(array("success" => "success""message" => $message));
    } else{

        echo 
    json_encode(array("success" => "fail","message"=>"Unable to send email. Please try again."));
    If I enable the second $headers line, as shown below, no email is sent at all!!
    Any ideas why that would be?
    PHP:
    $headers "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: " $sender;

    // send the message with mail()
    if(mail($recipient$message_subject$emailmessage$headers)){

            
    $message $message "We have sent you an email to confirm your address. Please follow the instructions in the email to complete your membership."
            echo 
    json_encode(array("success" => "success""message" => $message));
    } else{

        echo 
    json_encode(array("success" => "fail","message"=>"Unable to send email. Please try again."));
    }    
     
  2. ztk.me

    ztk.me Well-Known Member HowtoForge Supporter

    first: you don't send html emails, html emails are no use, nobody sane has html mails activated in his client. At least send the alternative text in a readable format unlike one big hosting company with an red H is not capable of. You don't want your readers think you're incompetent.

    secondly, don't use php-mail() function.... say what u want but please just don't use it. I'm more serious about this than my fist point.

    and last: use swiftmailer or anything else to boost not only your performance in deploying but you could also look at how they do it (right) if you are interested.

    Basically you use the same code which is all around google, and 99% of ppl can't be wrong right? No you say because doesn't work for you... you may check the restrictions on your server, maybe you're not allowed to FAKE your From-Header or whatever it is, its probably not the code.

    Just don't use mail() - use authenticated imap/smtp either from your server or using external services if you don't want to pay about 250€/month at minimum to get some better reputation for your sending IP....
     
    Last edited: Jul 20, 2018
  3. Diego M. Rodrigues

    Diego M. Rodrigues New Member

    You can use PHPMailer - A full-featured email creation and transfer class for PHP.
     
  4. nickdawhick

    nickdawhick New Member

    Is there no less expensive option?!
     
  5. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Like @ztk.me wrote.
     

Share This Page