Jump to content

[SOLVED] PHP mail problem


sean14592

Recommended Posts

Hi, I'm having a problem with php mail. I can seem to set the 'from' thing, so everytime my email gets sent it currently says"From:domain@borden.nswebhost.com" when it should say "From Support@********.com".

 

Here is my code:

 

// ---------------- SEND MAIL FORM ---------------- 

// send e-mail to ...
$to=$email_to; 

// Your subject 
$subject="******** - Your New Password!"; 

// From 
$from ='******** <support@********.com>';

//Message
$name = $rows['name'];
$messages = "<img src=\"".$emailheaderimage."\" /><br><br>";
$messages.="Dear $name <br>";
$messages.="You have requested to reset your password on ".$sitedomain.". If you did not request this, please ignore it.<br><br>";
$messages.="<b> Your new password is $new_password </b><br><br>";
$messages.="To login with your new password, please visit the following page:<br>";
$messages.="<a href=\"".$siteurl."index.php?p=ologin\">".$siteurl."index.php?p=ologin</a><br><br>";
$messages.="Thank You<br> The ******* Team ";

$mailheaders = 'Content-Type: text/html; charset=ISO-8859-1\r\n';


// send email 
$sentmail = mail($to,$from,$subject,$messages,$mailheaders); 
}

 

Cheers

Sean

 

p.s ******** == My domain

Link to comment
Share on other sites

Hi, thanks for elping, I have chaged what you said, bu still dosent work, btw....

 

$sentmail = mail($to,$from,$subject,$messages,$mailheaders);

 

Is supposed to be....

$sentmail = mail($to,$subject,$messages,$mailheaders);

 

Is that right?, If it is he first one I get errors.

 

 

 

I really, really need to fix this, Any help would be very greatful. Cheers, Sean

Link to comment
Share on other sites

from is supposed to be in the headers.


// ---------------- SEND MAIL FORM ---------------- 

// send e-mail to ...
$to=$email_to; 

// Your subject 
$subject="******** - Your New Password!"; 

// From 
$from ='******** <support@********.com>';

//Message
$name = $rows['name'];
$messages = "<img src=\"".$emailheaderimage."\" /><br><br>";
$messages.="Dear $name <br>";
$messages.="You have requested to reset your password on ".$sitedomain.". If you did not request this, please ignore it.<br><br>";
$messages.="<b> Your new password is $new_password </b><br><br>";
$messages.="To login with your new password, please visit the following page:<br>";
$messages.="<a href=\"".$siteurl."index.php?p=ologin\">".$siteurl."index.php?p=ologin</a><br><br>";
$messages.="Thank You<br> The *********** Team ";
$mailheaders = "From: ".$from;
$mailheaders .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';


// send email 
$sentmail = mail($to,$subject,$messages,$mailheaders); 
}

Link to comment
Share on other sites

Is that right?, If it is he first one I get errors.

 

No thats wrong

 

see this line

// From

$from ='******** <support@********.com>';

 

update

$from ='******** <support@********.com>';

to

$from ='support@********.com <support@********.com>';

Link to comment
Share on other sites

jonsjava, when I do wha you aid, I get the email to...

 

<img src="http://www.*****.com/images/email_header.gif" /><br><br>Dear Sean Preston <br>You have requested to reset your password on *****. If you did not request this, please ignore it.<br><br><b> Your new password is 60ecfe02 </b><br><br>To login with your new password, please visit the following page:<br><a href="http://www.*****.com/index.php?p=ologin">http://www.*****.com/index.php?p=ologin</a><br><br>Thank You<br> The *****Team 

 

Ummm, so its not seeing the headers as headers, so its showing text not html.

The actual from par of the email when eceives has all the ehaders in it, if you get me.

basically, the code is deactivating the Content-Type headers

 

Cheers

Sean

Link to comment
Share on other sites

one solution is the MIME mail class:

MIME.class.php

<?php
/* ---------------------------------------------------------
   MIME Class:
   Allows creation of e-mail messages via the MIME Standard.
   The class supports multiple attachments and presenting 
   an e-mail in HTML.
--------------------------------------------------------- */
include ("MIME.def");

class MIME_mail {
//public:
 var $to;
 var $from;
 var $subject;
 var $body;
 var $headers = "";
 var $errstr="";

 // these are the names of the encoding functions, user
 // provide the names of user-defined functions

 var $base64_func= '';		# if !specified use PHP's base64  
 var $qp_func = '';      	# None at this time

 // If do not have a local mailer..use this array to pass info of an SMTP object
 // e.g. $mime_mail->mailer = array('name' => 'smtp', method => 'smtp_send()');
 // 'name' is the name of the object less the $ and 'method' can have parameters
 // specific to itself.  If you are using MIME_mail object's to, from, etc.
 // remember to send parameters a literal strings referring 'this' object!!!!
 // If in doubt, you are probably better off subclassing this class...
 var $mailer = "";	# Set this to the name of a valid mail object

//private:
 var $mimeparts = array(); 

// Constructor.
function MIME_mail($from="", $to="", $subject="", $body="", $headers = "") {
$this->to = $to;
$this->from = $from;
$this->subject = $subject;
$this->body = $body;
if (is_array($headers)) {
	if (sizeof($headers)>1) 
		$headers=join(CRLF, $headers);
	else
		$headers=$headers[0];
}
if ($from) {
$headers = preg_replace("!(from:\ ?.+?[\r\n]?\b)!i", '', $headers);
}
$this->headers = chop($headers);
$this->mimeparts[] = "" ;	//Bump up location 0;
$this->errstr = "";
return;
}

/* ---------------------------------------------------------
 Attach a 'file' to e-mail message
 Pass a file name to attach.
 This function returns a success/failure code/key of current
 attachment in array (+1). Read attach() below.
--------------------------------------------------------- */
function fattach($path, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
$this->errstr = "";
if (!file_exists($path)) {
	$this->errstr = "File does not exist";
	return 0;
}
// Read in file
$fp = fopen($path, "rb");	# (b)inary for Win compatability
if (!$fp) {
	$this->errstr = "fopen() failed";
	return 0;	//failed
}
$contenttype .= ";\r\n\tname=".basename($path);
$data = fread($fp, filesize($path));
return $this->attach($data, 
		$description, 
		$contenttype,
		$encoding,
		$disp);	
}

/* ---------------------------------------------------------
 Attach data provided by user (rather than a file)
 Useful when you want to MIME encode user input
 like HTML. NOTE: This function returns key at which the requested
 data is attached. IT IS CURRENT KEY VALUE + 1!!
 Construct the body with MIME parts
--------------------------------------------------------- */
function attach($data, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
$this->errstr = "";
if (empty($data)) {
	$this->errstr = "No data to be attached";
	return 0;
}
if (trim($contenttype) == '') $contenttype = OCTET ;
if (trim($encoding) == '') $encoding = BASE64;
if ($encoding == BIT7) $emsg = $data;
elseif ($encoding == QP) 
	$emsg = $$this->qp_func($data);
elseif ($encoding == BASE64) {
	if (!$this->base64_func) 	# Check if there is user-defined function
		$emsg = base64_encode($data);
	else 
		$emsg = $$this->base64_func($data);
}
$emsg = chunk_split($emsg);
//Check if content-type is text/plain and if charset is not specified append default CHARSET
if (preg_match("!^".TEXT."!i", $contenttype) && !preg_match("!;charset=!i", $contenttype)) 
	$contenttype .= ";\r\n\tcharset=".CHARSET ;
$msg = sprintf("Content-Type: %sContent-Transfer-Encoding: %s%s%s%s",
$contenttype.CRLF, 
$encoding.CRLF,
((($description) && (BODY != $description))?"Content-Description: $description".CRLF:""),
($disp?"Content-Disposition: $disp".CRLF:""),
CRLF.$emsg.CRLF);
BODY==$description? $this->mimeparts[0] = $msg: $this->mimeparts[] = $msg ;
return sizeof($this->mimeparts);
}

/* ---------------------------------------------------------
 private:
 Construct mail message header from info already given.
 This is a very important function.  It shows how exactly
 the MIME message is constructed.
--------------------------------------------------------- */
function build_message() {


$this->errstr = "";
$msg = "";
$boundary = 'PM'.chr(rand(65, 91)).'------'.md5(uniqid(rand()));	# Boundary marker
$nparts = sizeof($this->mimeparts);

// Case 1: Attachment list is there.  Therefore MIME Message header must have multipart/mixed
	if (is_array($this->mimeparts) && ($nparts > 1)):
	$c_ver = "MIME-Version: 1.0".CRLF;
	$c_type = 'Content-Type: multipart/mixed;'.CRLF."\tboundary=\"$boundary\"".CRLF;
	$c_enc = "Content-Transfer-Encoding: ".BIT7.CRLF;
	$c_desc = $c_desc?"Content-Description: $c_desc".CRLF:"";
	$warning = CRLF.WARNING.CRLF.CRLF ;

// Since we are here, it means we do have attachments => body must become an attachment too.
	if (!empty($this->body)) {
		$this->attach($this->body, BODY, TEXT, BIT7);
	}

// Now create the MIME parts of the email!
	for ($i=0 ; $i < $nparts; $i++) {
		if (!empty($this->mimeparts[$i])) 
     		  $msg .= CRLF.'--'.$boundary.CRLF.$this->mimeparts[$i].CRLF;
	}
	$msg .= '--'.$boundary.'--'.CRLF;
	$msg = $c_ver.$c_type.$c_enc.$c_desc.$warning.$msg;
else:
	if (!empty($this->body)) $msg .= $this->body.CRLF.CRLF;
endif;
return $msg;
}


/* ---------------------------------------------------------
 public:
 Now Generate the entire Mail Message, header and body et al.
--------------------------------------------------------- */
function gen_email($force=false) {

$this->errstr = "";
if (!empty($this->email) && !$force) return $this->email ;  // saves processing
$email = "";
if (empty($this->subject)) $this->subject = NOSUBJECT;
if (!empty($this->from)) $email .= 'From: '.$this->from.CRLF;
if (!empty($this->headers)) $email .= $this->headers.CRLF;
$email .= $this->build_message();
$this->email = $email;
return $this->email;
}

/* ---------------------------------------------------------
 public:
 Printable form
--------------------------------------------------------- */
function print_mail($force=false) {
$this->errstr = "";
$email =  $this->gen_email($force);
if (!empty($this->to)) $email = 'To: '.$this->to.CRLF.$email;
if (!empty($this->subject)) $email = 'Subject: '.$this->subject.CRLF.$email;
print $email;
}

/* ---------------------------------------------------------
 public:
 Send mail via local mailer
--------------------------------------------------------- */
function send_mail($force=false) {
$this->errstr = "";
$email = $this->gen_email($force);
if (empty($this->to)) {
	$this->errstr = "To Address not specified";
	return 0;
}
if (is_array($this->mailer) && (1 ==  sizeof($this->mailer)) ) {
	$mail_obj = $this->mailer['name'];
	$mail_method = $this->mailer['method'];
	if (empty($mail_obj)) {
		$this->errstr = "Invalid object name passed to send_mail()";
		return 0;
	}
	global $mail_obj;
	eval("$ret = \$$mail_obj".'->'."$mail_method;");
	return $ret;
}	
return mail($this->to, $this->subject, "", $email);
}
} // Class End

MIME.def

<?php
/* -----------------------------------------------------------
  Constants used inside the class.   This file is included
  by the class module and these constants an be freely
  used in scripts using MIME_mail class
----------------------------------------------------------- */

define('BASE64', 'base64');
define('BIT7', '7bit');
define('QP', 'quoted_printable');
define('NOSUBJECT', '(No Subject)');
define('WARNING', 'This is a MIME encoded message');
define('OCTET', 'application/octet-stream');
define('TEXT', 'text/plain');
define('HTML', 'text/html');
define('JPEG', 'image/jpg');
define('GIF', 'image/gif');
define('CRLF', "\r\n");
define('CHARSET', 'us-ascii');
define('INLINE', 'inline');
define('ATTACH', 'attachment');
define('BODY', CRLF.'BODY'.CRLF);
?>

and to use it:

// ---------------- SEND MAIL FORM ---------------- 

// send e-mail to ...
$to=$email_to; 

// Your subject 
$subject="******** - Your New Password!"; 

// From 
$from ='******** <support@********.com>';

//Message
$name = $rows['name'];
$messages = "<img src=\"".$emailheaderimage."\" /><br><br>";
$messages.="Dear $name <br>";
$messages.="You have requested to reset your password on ".$sitedomain.". If you did not request this, please ignore it.<br><br>";
$messages.="<b> Your new password is $new_password </b><br><br>";
$messages.="To login with your new password, please visit the following page:<br>";
$messages.="<a href=\"".$siteurl."index.php?p=ologin\">".$siteurl."index.php?p=ologin</a><br><br>";
$messages.="Thank You<br> The ********** Team ";
// send email 
include("MIME.class.php");
$mime = new MIME_mail($from, $to, $subject);
$mime->attach($messages, "", HTML, BASE64);
$mime->send_mail();
}

Link to comment
Share on other sites

Hi

 

<img src="http://www.****.com/images/email_header.gif" />

Dear Sean Preston <br>You have requested to reset your password on ****.com. If you did not request this, please ignore it.

<b> Your new password is afe2bf87 </b>

To login with your new password, please visit the following page:
<a href="http://www.****.com/index.php?p=ologin">http://www.********.com/index.php?p=ologin</a>

Thank You 
The H**** Team 

 

So, now the new lines are there, but the images and links do appear, so the html is still disabled in the content tag.

 

Cheers

Sean

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.