Link to home
Start Free TrialLog in
Avatar of jaxbrian
jaxbrian

asked on

.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
Avatar of hielo
hielo
Flag of Wallis and Futuna image

<?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);
?>
Avatar of jaxbrian
jaxbrian

ASKER

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
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;
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

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

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
>>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

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
thanks a bunch