Reputation: 558
I had put a PHP email system up, and that's working fine. When I send a message it comes up as "[email protected]" How could I get it so when someones opens the email it's from my company name? Here's my PHP. Thanks in advance -Ben
<?php
$headers= "Inquiry: " . $_POST['inquiry'] . "\r\n" .
// ect etc
"Contact: " . $_POST['contact'];
"BCC: " . $_POST['email'];
$from = "[email protected]";
$header1 = " ";
$header2 = " ";
$to_myself = "";
$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
$thank_you = " ".$common_data;
mail($to_myself, "Your Memory", $common_data, $header1);
mail($to_visitor, "Your Memory", $thank_you, $header2); ?>
Upvotes: 0
Views: 209
Reputation: 6190
I am using a class. It's much more easier. You can try this too. I took this class from somewhere and I improved it to match with my requirement.
<?php
class Mail
{
private $arrTo = array();
private $arrCC = array();
private $arrBCC = array();
private $strContent = "";
private $strSubject = "";
private $arrHeader = array();
private $arrAttachments = array();
private $numPriority = 3;
private $strCharSet = "iso-8859-1";
private $strContentType = "text/plain";
private $strEncoding = "8bit";
/*
* Mail::addUser()
*
* Adds a user to send the email to
*
* @param String $strAddress : Holds an email address
*
* @access public
*
* @return Integer
*/
public function addUser($strAddress){
if($this->emailCheck($strAddress)){
$this->arrTo[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
* Mail::removeUser()
*
* Removes an address from the array
*
* @param String $strAddress : Holds an email address
*
* @access public
*
* @return void
*/
public function removeUser($strAddress){
//create array or arrays to check
$arrVars = array('arrTo', 'arrCC', 'arrBCC');
foreach($arrVars as $resVars){
//sees if the item exists in the array
$numKey = array_search($strAddress, $this->{$resVars});
if(isset($numKey)){
//remove the email address
$this->{$resVars}[$numKey] = "";
}
unset($numKey);
}
}
/*
* Mail::addCC()
*
* Adds a CC user to send to
*
* @param String $strAddress : Holds an email address
*
* @access public
*
* @return Integer
*/
public function addCC($strAddress){
if($this->emailCheck($strAddress)){
$this->arrCC[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
* Mail::addBCC()
*
* Adds a BCC user to send to
*
* @param String $strAddress : Holds an email address
*
* @access public
*
* @return Integer
*/
public function addBCC($strAddress){
if($this->emailCheck($strAddress)){
$this->arrBCC[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
* Mail::addReplyTo()
*
* Adds a reply-to email address
*
* @param String $strEmail : Holds the reply-to email address
*
* @access public
*
* @return void
*/
public function addReplyTo($strEmail){
if($this->emailCheck($strEmail)){
$this->addHeader("Reply-to: $strEmail");
}
}
/*
* Mail::setContent()
*
* Adds the content of the email
*
* @param String $strText : Holds the email content
*
* @access public
*
* @return void
*/
public function setContent($strText){
$this->strContent = $strText;
}
/*
* Mail::setSubject()
*
* Adds the email subject
*
* @param String $strText : Holds the email subject
*
* @access public
*
* @return void
*/
public function setSubject($strText){
$this->strSubject = $strText;
}
/*
* Mail::setFrom()
*
* Adds the sender info to the headers
*
* @param String $strText : Holds the details of who the email is from
*
* @access public
*
* @return void
*/
public function setFrom($strText){
$this->addHeader("From: $strText");
}
/*
* Mail::setHTML()
*
* Sets the email to allow html
*
* @access public
*
* @return void
*/
public function setHTML(){
$this->strContentType = "text/html";
}
/*
* Mail::setPlain()
*
* Sets the email to plain text
*
* @access public
*
* @return void
*/
public function setPlain(){
$this->strContentType = "text/plain";
}
/*
* Mail::setPriority()
*
* Sets the email priority from 1 – Urgent to 5 – not so
*
* @param Integer $numPriority : The email priority
*
* @access public
*
* @return void
*/
public function setPriority($numPriority){
$this->numPriority = $numPriority;
}
/*
* Mail::addReadConfirmationEmail()
*
* Ensures that a confirmation email is sent when read
*
* @param String $strEmail : Email address to send read confirmation to
*
* @access public
*
* @return void
*/
public function addReadConfirmationEmail($strEmail){
if($this->emailCheck($strEmail)){
$this->addHeader("Disposition-Notification-To: <$strEmail>");
}
}
/*
* Mail::addHeader()
*
* Adds the header details
*
* @param String $strText : Holds the header details
*
* @access public
*
* @return void
*/
public function addHeader($strText){
$this->arrHeader[] = $strText;
}
/*
* Mail::addAttachment()
*
* Adds an attachment to be sent with the email
*
* @param String $strFile : The file that you want to attach
* @param String $strName : The name of the attachment you want displayed
* @param String $strType : The MIME type of the file
*
* @access public
*
* @return void
*/
public function addAttachment($strFile, $strName, $strType){
//check to make sure the file exists
if(file_exists($strFile)){
$this->arrAttachments[] = array('path' => $strFile, 'name' => $strName, 'type' => $strType);
}
}
/*
* Mail::send()
*
* Sends the email to all the required people
*
* @access public
*
* @return void
*/
public function send(){
//get all the emails in a string to use
$strTo = implode(", ", $this->arrTo);
//add any CC addresses if needed
if($this->arrCC){
$this->addHeader("Cc: ".implode(", ", $this->arrCC));
}
//add any BCC addresses if needed
if($this->arrBCC){
$this->addHeader("Bcc: ".implode(", ", $this->arrBCC));
}
//append any attachments to the end of the content
if(count($this->arrAttachments)){
$this->appendAttachments();
}
//add the additional headers
$this->addAdditionalHeaders();
//implode the headers as they need to be in a single string
$strHeader = implode("\r\n", $this->arrHeader);
// Set this if you have your own domain
//ini_set('sendmail_from', 'yourdomain.com');
//send the email
mail($strTo, $this->strSubject, $this->strContent, $strHeader);
}
/*
* Mail::appendAttachments()
*
* Appends any attachments to the end of the email content
*
* @access public
*
* @return void
*/
public function appendAttachments(){
//create a unique boundary value
$strBoundary = "H2O-".time();
//add the boundary to the headers
$this->addHeader("Content-Type: multipart/alternitive; boundary=$strBoundary");
//if there is content to the email already we need to add the boundary and content type
if($this->strContent){
$strContent = "–$strBoundary\r\n";
$strContent .= "Content-Transfer-Encoding: {$this->strEncoding}\r\n";
$strContent .= "Content-type: {$this->strContentType}; charset={$this->strCharSet}\r\n\r\n";
$this->strContent = $strContent.$this->strContent."\r\n\r\n\r\n";
}
//loop through all the attachments
foreach($this->arrAttachments as $arrFile){
//read the content of the file into a string and base64 encode and split into the correct chunk size
$strData = chunk_split(base64_encode(implode("", file($arrFile['path']))));
//add the attachments
$strAttachment = "–$strBoundary\r\n";
$strAttachment .= "Content-Transfer-Encoding: base64\r\n";
$strAttachment .= "Content-Type: {$arrFile['type']}; name={$arrFile['name']}\r\n";
$strAttachment .= "Content-Disposition: attachment; filename={$arrFile['name']}\r\n\r\n";
$strAttachment .= "$strData\r\n";
//add the attachment to the email content
$this->strContent .= $strAttachment;
unset($strAttachment);
}
//add the closing boundary
$this->strContent .= "–$strBoundary–";
}
/*
* Mail::addAdditionalHeaders()
*
* Adds additional headers for content type and priority etc
*
* @access public
*
* @return void
*/
public function addAdditionalHeaders(){
$this->addHeader("MIME-Version: 1.0");
//add the content type and charset if there are no attachments
if(!count($this->arrAttachments)){
$this->addHeader("Content-type: {$this->strContentType}; charset={$this->strCharSet}");
}
//add the priority
$this->addHeader("X-Priority: $this->numPriority");
}
/*
* Mail::emailCheck()
*
* Checks to ensure that the email address is valid
*
* @param String $strAddress : Holds an email address
*
* @access public
*
* @return Integer
*/
public function emailCheck($strAddress){
if(ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $strAddress)){
return 1;
}else{
return 0;
}
}
}
?>
Implementation:
$email = new Mail();
$email->addUser($toAddress);
$email->setFrom("Your Company Name<" . $yourCompanyEMail. ">");
$email->setHTML();
$email->setSubject("Subject goes here");
$email->setContent("Mail body goes here");
$email->send();
Cheers !
Upvotes: 0
Reputation: 29991
Try this:
$headers = "Contact: ".$_POST['contact']."\r\n";
$headers .= "BCC: ".$_POST['email']."\r\n";
$headers .= "From: Your Company <[email protected]>\r\n";
mail($to_visitor, "Your Memory", $thank_you, $headers);
You should also read the documentation
Upvotes: 2