Search code examples
emailheaderphpmailerreturn-path

How to set a custom header using phpmailer


I am using phpmailer for sending emails, but I want to make a custom header for my company, by adding a textarea field that contain any custom header for example using a header like this one:

exemple of header or any other header types.. How can I do this , thanks in advance.


Solution

  • You will need to do some discovery and modification of the default headers set by PHPmailer for this to be achieved.

    You will need to use different functions to set/edit headers depending on the type of header you want to set/edit. Here are a few examples:

    From:

    $mail->setFrom('[email protected]', 'Mailer');
    

    Subject:

    $mail->Subject = 'Here is the subject';
    

    Custom:

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

    In short, It is quite an effort to do this when the headers are free-typed in a text box. It is however doable with detection and validation on your side.