Adding Header to PHPMailer

Discussion in 'PHP' started by schandak2001, Feb 13, 2017.

  1. #1
    I want to Below Header to my function , How and where to add it ?

    $headers="";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


    My Function is : ( Where and How to add above Lines )

    function sendEmail($email,$subject, $message)
    {

    $mail = null;
    $mail = new PHPMailer(true);
    // $mail->IsSMTP();
    try
    {
    $mail->Host = "Host"; // SMTP server
    $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
    $mail->SMTPAuth = true; // enable SMTP authentication
    // sets the SMTP server
    $mail->Port = 25; // set the SMTP port for the GMAIL server
    $mail->Username ="username"; // SMTP account username
    $mail->Password = "apaaa"; // SMTP account password
    $mail->Priority = "1";
    $mail->ConfirmReadingTo = "";

    $mail->AddAddress($email);

    $mail->SetFrom('', 'XXXX');
    $mail->Subject = $subject;
    $mail->MsgHTML($message);


    if(!$mail->Send())
    {
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
    }

    //echo "Message has been sent";
    }
    catch (Exception $e)
    {
    echo $e;
    }
    $mail->ClearAllRecipients();
    }
    ?>
     
    schandak2001, Feb 13, 2017 IP
  2. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #2
    PHPMailer has a feature to add custom headers as defined here:
    https://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addCustomHeader

    Examples:
    $mail->addCustomHeader('X-custom-header', 'custom-value');
    OR
    $mail->addCustomHeader('X-custom-header: custom-value');

    So for you, you can add:
    $mail->addCustomHeader('MIME-Version: 1.0');
    $mail->addCustomHeader('Content-Type: text/html; charset=ISO-8859-1');
     
    Tanya Roberts, Feb 18, 2017 IP
  3. schandak2001

    schandak2001 Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Thanks a Lot.
     
    schandak2001, Feb 23, 2017 IP