How to change address by mail php mail sender
To change the sender's address in a PHP mail sender, you can use the From
header in the email headers. Here's an example:
<?php
// Set the sender's email address and name
$from_email = '[email protected]';
$from_name = 'New Sender';
// Set the recipient's email address
$to_email = '[email protected]';
// Set the subject and body of the email
$subject = 'Test Email';
$body = 'This is a test email';
// Create a new PHPMailer object
$mail = new PHPMailer();
// Set the sender's email address and name
$mail->From = $from_email;
$mail->FromName = $from_name;
// Set the recipient's email address
$mail->addAddress($to_email);
// Set the subject and body of the email
$mail->Subject = $subject;
$mail->Body = $body;
// Send the email
$mail->send();
?>
In this example, we're using the PHPMailer library to send the email. We set the From
header using the From
property, and set the FromName
property to specify the sender's name.
If you're using the built-in PHP mail()
function, you can use the From
header in a similar way:
<?php
// Set the sender's email address and name
$from_email = '[email protected]';
$from_name = 'New Sender';
// Set the recipient's email address
$to_email = '[email protected]';
// Set the subject and body of the email
$subject = 'Test Email';
$body = 'This is a test email';
// Create the email headers
$headers = array(
'From: '. $from_name. ' <'. $from_email. '>',
'Reply-To: '. $from_email,
'MIME-Version: 1.0',
'Content-Type: text/plain; charset=UTF-8'
);
// Create the email body
$message = $body;
// Send the email
mail($to_email, $subject, $message, implode("\r\n", $headers));
?>
In this example, we're creating an array of email headers using the From
header, and then sending the email using the mail()
function.