Set envelope from in php

Setting The From Envelope When Sending Emails In PHP.

When sending email using the mail() function in PHP you will need to ensure that you are setting a valid email 'envelope' from address. If this is not specified then you may find that email is not sent or delivered as this is a feature that spammers unfortunately can and do abuse.

An email has two types of 'From' address associated with it when being sent. These are 'Header From address' and 'Envelope From address'. The best way to understand these is to view the header from address as being an address you would place at the top of a letter when contacting someone and the envelope from address as being what you would write on the back of an envelope as being your return address. Essentially the main reason for having 2 addresses is because the 'envelope' from address is what computers use in the case of bounce error messages and the header from address is what is used for the recipient to respond back to.

Magento

If you are using Magento then this configuration option can be set in the admin interface. Login to your admin panel and goto

advanced -> system config

On the config page look for option set return path set it to yes and Magento will add in additional parameters for your configured mail address and this will correct the mail-from and envelope-from mail headers.

WordPress (PHPMailer Class)

Depending on your version of wordpress, you may be able to edit the class-phpmailer.php file.

File location: wp-includes/class-phpmailer.php

Change the following variable

public $Sender = ''
To
public $Sender = 'youremail@yourdomain.tld'

Windows

On windows you must use the below to set the from address properly by specifying the ini_set command.

ini_set("sendmail_from", 'admin@yourdomain.tld');

Linux

On Linux the PHP mail() command has a 5th argument and you should set this as below:-

mail('to@blah.com','subject!','body!','From: from@blah.com','-f from@blah.com');

You can also check on this with the below example.
Create a page named test.php and add the below text, then upload and test, when visiting the page you can then input some data, the from and to address is the same here but this can be set to anything.

The below code is provided as-is without support and as an example only to illustrate usage of an envelope from address.

Windows Server Example

<?php 
if ($_POST["email"]<>'') {
$ToEmail = $_POST["email"];
$FromEmail='myemail@domain.tld';
ini_set("sendmail_from",$FromEmail);
$EmailSubject = 'Test Email Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]." \n ";
$MESSAGE_BODY .= "Email: ".$_POST["email"]." \n ";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])." \n ";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader,$FromEmail) or die ("Failure");
?>
Your message was sent
<a href="test.php">Go Back<a>
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr> <tr> <td class="bodytext"> </td> <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr> </table> </form> <?php }; ?>

Linux Server Example

<?php
if ($_SERVER['QUERY_STRING'] == 'send') {

$ToEmail = $_POST["email"];
$FromEmail = "-f ".$_POST["email"];
$EmailSubject = 'Test Email Form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]." \n ";
$MESSAGE_BODY .= "Email: ".$_POST["email"]." \n ";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])." \n ";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader, $FromEmail) or die ("Failure");
echo "Your message was sent <a href='test.php'>Go Back<a><br><br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Email Form</title>>
</head>
<body>
<form action="?send" method="post">
<table width="500" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" value="test" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" value="admin@yourdomain.tld" size="32"></td>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext">This is a test Email</textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send Email"></td>
</tr>
</table>
</form>
</body>
</html>
        
  • 66 Users Found This Useful
Was this answer helpful?

Related Articles

What Are My Email Account Connection Details?

When setting up your email accounts in your email client (Outlook,Thunderbird,Mac Mail...

Do you provide a webmail facility?

Yes we do. All email accounts setup with us have webmail access. You can...

What are my webmail login details?

Your webmail login details are the same as your email account pop3/imap details....

Configure Android Phone

To Setup your email account on your android phone you will need to follow the steps...

Configure iOS Phone

Automatic Configuration You can use our automatic configuration utility for any IOS device here...