.html document, preg_replace() mail php?

I saw someone that had a file that just had FIRSTNAME at the top and an automated response below it so they could send out to people that sent them messages. I am wanting to explore that area. I know that I could do a preg_replace()  to replace the first name with whatever I would want. but, how would i call the file and then edit it and then send it within php?

thank you for any input on this

B
LVL 2
jaxbrianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
<?php

$name = 'John Smith';

//get the file
$content=file_get_contents("yourFile.html");

//now replace whatever you need to replace
$content = str_replace('FIRSTNAME',$name, $content);


//now the emailing part
$to="You@yourcompany.com";
$subject="Testing";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $content, $headers);
?>
0
jaxbrianAuthor Commented:
I am still messin with it. It didnt work straight out of the box. if I can not get it to work in the next few minutes I will post what errors I am receiving
thank you
Brian
0
hieloCommented:
save the above example as test.php into the SAME folder where you have "yourFile.html".
Then withing yourFile.html you need to have a "place holder" named "FIRSTNAME" (all in uppercase).

to verify that FIRSTNAME was replaced as expected, after this line:
$content = str_replace('FIRSTNAME',$name, $content);

put:
echo $content;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jaxbrianAuthor Commented:
this is what it says when I try to do it that way. I did incude the info that I need to send an email to the test file. Should I not have? this is the error message


Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\deadcentre\webroot\mailtest102.php on line 28

any ideas

below is the code that i usually use to send emails from a form i have on one of my sites. can I just add some of the other infor to it?
<?PHP

if(isset($_POST["Submit"]))
{
require("c:\php\includes\class.phpmailer.php");

$mail = new PHPMailer();

////////////////////////////////////////////////////////////////
// Customize the following 5 lines with your own information. //
////////////////////////////////////////////////////////////////

$toaddress = "my email"; //Change this to the email address you will be receiving your notices.
$mailhost = "mymail.brinkster.com";  //Change this to your actual Domain name.
$fromaddress = "host email";  //Change this to the email address you will use to send and authenticate with.
$frompwd = "PWD";  //Change this to the above email addresses password.
$subject = "the site has sent you a message";  //Change this to your own email message subject.

//////////////////////////////////////////
// DO NOT CHANGE ANYTHING PAST THIS LINE//
//////////////////////////////////////////

$fromname = $_POST["Tname"];
$question = $_POST["Tquestion"] ;
$rplyto = $_POST["Temail"];
$msgbody = $fromname . "<br>" . $rplyto . "<br>" . $question;

$mail->IsSMTP();
$mail->Host = $mailhost;
$mail->SMTPAuth = true;
$mail->Username = $fromaddress;
$mail->Password = $frompwd;

$mail->From = $fromaddress;
$mail->FromName = $fromname;
$mail->AddReplyTo($rplyto); 
$mail->AddAddress($toaddress); 
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msgbody;
$URL="http://www.MYSITE.html";  header ("Location: $URL");

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
 
echo "Thank you, your message has been sent!";
}
mail($rplyto, $subject, $msgbody);
?>

Open in new window

0
jaxbrianAuthor Commented:
here is the code that gave me the error message.



thank you
B
<?php
include("test2223.php");
include("class.phpmailer.php");
$name = 'John Smith';

//get the file
$content=file_get_contents("thankyouemail.html");

//now replace whatever you need to replace
$content = str_replace('FIRSTNAME',$name, $content);


//now the emailing part
$to="jaxbrian@hotmail.com.com";
$subject="Testing";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";


// Mail it
mail($to, $subject, $content, $headers);
?>

Open in new window

0
jaxbrianAuthor Commented:
When I echo $content, it has changed the FIRSTNAME to john smith like you defined. so that part is working great. thank you. I guess I need to figure out why it is not mailing
0
hieloCommented:
>>I guess I need to figure out why it is not mailing
Because the machine where the script is running does NOT have a mail SERVER. You will have a remote mail server, which is what your working example does.
<?php
include("test2223.php");

$name = 'John Smith';
//get the file
$msgbody=file_get_contents("thankyouemail.html");

//now replace whatever you need to replace
$msgbody = str_replace('FIRSTNAME',$name, $msgbody);


include("class.phpmailer.php");
////////////////////////////////////////////////////////////////
// Customize the following 5 lines with your own information. //
////////////////////////////////////////////////////////////////

$toaddress = "youremail here"; //Change this to the email address you will be receiving your notices.
$mailhost = "mymail.brinkster.com";  //Change this to your actual Domain name.
$fromaddress = "host email";  //Change this to the email address you will use to send and authenticate with.
$frompwd = "PWD";  //Change this to the above email addresses password.
$subject = "the site has sent you a message";  //Change this to your own email message subject.
$fromname="yourname here";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = $mailhost;
$mail->SMTPAuth = true;
$mail->Username = $fromaddress;
$mail->Password = $frompwd;

$mail->From = $fromaddress;
$mail->FromName = $fromname;

$mail->AddAddress($toaddress); 
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msgbody;






// Mail it
$mail->Send()
?>

Open in new window

0
jaxbrianAuthor Commented:
I stuck the $content and string replace stuff into another script i have that sends an email and intom y data base. it did send the email to me but it gave me an error message.

Thank you, your message has been sent!
Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\deadcentre\webroot\mailtest103.php on line 67

below is the code that I stuck the $content/string replace into. any suggestions to why it is erroring
thanks alot,
B


<?PHP

if(isset($_POST["Submit"]))
{
require("c:\php\includes\class.phpmailer.php");
include("test223.php");
include("test224.php");

$mail = new PHPMailer();



$fromname = htmlspecialchars($_POST["Tname"]);
$question = strip_tags($_POST["Tquestion"]);
$rplyto = htmlspecialchars($_POST["Temail"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$rplyto))
{
    die("E-mail address not valid");
}
$content=file_get_contents("thankyouemail.html");
$content = str_replace('FIRSTNAME',$fromname, $content);

$questions = addslashes($question);
$questions2 = htmlentities($questions);
$cleanquestion = addcslashes($questions2, "a");
$newstring = str_replace(array('=','/','[','/'),array('+','-','_','44'),$cleanquestion);
$msgbody = $content;
wordwrap($question, 50, "<br />\n"); 

$mail->IsSMTP();
$mail->Host = $mailhost;
$mail->SMTPAuth = true;
$mail->Username = $fromaddress;
$mail->Password = $frompwd;


$mail->From = $fromaddress;
$mail->FromName = $fromname;
$mail->AddReplyTo($rplyto); 
$mail->AddAddress($toaddress); 
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msgbody;



if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
 
echo "Thank you, your message has been sent!";
}


$sql="INSERT INTO mytable (name, email, question)
VALUES
('$_POST[Tname]','$_POST[Temail]','$newstring')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mail($rplyto, $subject, $msgbody);


?>

Open in new window

0
hieloCommented:
get rid of line 65. In your case, you cannot use the mail() function because you are sending email from a remote server. Your local server is NOT running a mail service.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaxbrianAuthor Commented:
I am going to award points for this and try a few things out. If i cant get it to work on the other servers I will be back. thanks a bunch you have really help me out in the last few days.


thank you

Brian
0
jaxbrianAuthor Commented:
thanks a bunch
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.