How to set a custom header using phpmailer

▼魔方 西西 提交于 2019-12-05 00:15:10

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('from@example.com', '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.

PHP Method PHPMailer::addCustomHeader (PHP Mailer Library)

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

ini_set('display_errors', 1);
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'example@gmail.com';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to
    $mail->Host = 'ssl://smtp.gmail.com:465';
    //Recipients
    $mail->setFrom('sentfrom@example.com', 'From SMTP Mailer');    
    $mail->addAddress('receiver@example.com');               // Name is optional
    $mail->addReplyTo('test@gmail.com', 'Reply');

    $mail->addCustomHeader('X-custom-header: custom-value');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b> and message';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

https://hotexamples.com/examples/-/PHPMailer/addCustomHeader/php-phpmailer-addcustomheader-method-examples.html

If you are using PHP mail function then try this:

additional_headers (optional) String or array to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.

If an array is passed, its keys are the header names and its values are the respective header values.

Note:

Before PHP 5.4.42 and 5.5.27, repectively, additional_headers did not have mail header injection protection. Therefore, users must make sure specified headers are safe and contains headers only. i.e. Never start mail body by putting multiple newlines.

Note:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

Note:

If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

Here is an example to send PHP Version in custom headers. Also If you need to find the custom headers in mail, you can find this info in RAW Headers.

Sometimes you need to access to email mailboxes. In most cases, this is done using the Internet Message Access Protocol, or IMAP. Read Article https://www.toptal.com/php/building-an-imap-email-client-with-php

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = array(
    'From' => 'webmaster@example.com',
    'Reply-To' => 'webmaster@example.com',
    'X-Mailer' => 'PHP/' . phpversion()
);

mail($to, $subject, $message, $headers);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!