Error with the ereg_replace [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 8 years ago.
When i place "&email&" in the message body in phpmailer the output message changes "&email&" to the e-mail address in the $to array. but it only uses the first email and it does see the rest. how do i make it get the rest emails and place it accordingly to the emails messages ?
$nq=0;
for($x=0; $x<$numemails; $x++)
{
$to = $allemails[$x];
if ($to)
{
$to = ereg_replace(" ", "", $to);
$message = ereg_replace("&email&", $to, $message);
$subject = ereg_replace("&email&", $to, $subject);
$qx=$x+1;
print "Line $qx . Sending mail to $to.......";
flush();
}
}
===
i can not post below is the image link :
http://filevault.org.uk/testee/mailer_image.png
Hope you'll understand now.

You shouldn't be using ereg_* anymore as it is deprecated - preg_replace is it's successor, though it looks like you only need str_replace anyway:
$message = str_replace("&email&",$to,$message);
If for some reason you really have to use ereg:
You may need the global flag g
ereg_replace("&email&g",
Different replacement every time
$to = array('email1#me.com','em2#me.com');
$text = 'asdkfjalsdkf &email& and then &email&';
$email_replacements = $to;
function replace_emails()
{
global $email_replacements;
return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}
var_dump(preg_replace_callback('#&email&#','replace_emails',$text));
//"asdkfjalsdkf email1#me.com and then em2#me.com"
Integrated:
$to = $allemails[$x];
$email_replacements = $to;
function replace_emails()
{
global $email_replacements;
return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}
if($to)
{
$message = preg_replace_callback('#&email&#','replace_emails',$message);
$subject = preg_replace_callback('#&email&#','replace_emails',$subject);
$qx=$x+1;
print "Line $qx . Sending mail to $to.......";
flush();

Related

How to hook inside a function in wordpress

I got a plugin (kind of sign up form), that offers developers some actions/hooks to add their own stuff. Inside the plugin the function is called like this:
// Allow devs to hook in
do_action( 'after_record_action', $result, $data, $format );
I guess that $data is an array storing the form data. After a visitor uses the signup form I want to send a mail containing $data using wp_mail()
How can I execute the following script using after_record_action? Do I need to add this inside my functions.php?
// get data from $data[] array
$data['email'] = $email;
$data['key'] = $key;
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
I appreciate any help in combining these, as I am not too experienced using php.
Add following code into your currently active theme's functions.php file:
add_action('after_record_action', 'marian_rick_custom_action', 10, 3);
function marian_rick_custom_action ($result, $data, $format){
// get data from $data[] array
$email = $data['email'];
$key = $data['key'];
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
}
if you are interested, how all this really works, check the official docs here

Form not sending, function eregi() is deprecated [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
Escape string to use in mail()
I am trying send my form, but I get this error...
Deprecated: Function eregi() is deprecated
I tried replacing it with preg_match(), but no luck. Here is my code:
$all_valid = $name_valid = $email_valid = $comments_valid = true;
if (isset($_POST['submit'])) {
if ($_POST['name'] == '') {
$all_valid = $name_valid = false;
}
if ($_POST['comments'] == '') {
$all_valid = $comments_valid = false;
}
if (!$validator->check_email_address($_POST['email'])) {
$all_valid = $email_valid = false;
}
if ($all_valid) {
// #### NO PROBLEMS FOUND - PROCESS THE FORM DATA HERE
$mail_to = 'cat30#hotmail.com'; // recipient address
$subject = "Email from website"; // email message subject line
$name = mysql_real_escape_string(trim($_POST['name'])); // sanitize the name
if (eregi("\r",$name) || eregi("\n",$name)){ // avoid email header injection
die();
}
$mail_from = mysql_real_escape_string(trim($_POST['email'])); // sanitize their email address
if (eregi("\r",$mail_from) || eregi("\n",$mail_from)){ // avoid email header injection
die();
}
$comments = htmlspecialchars(trim($_POST['comments'])); // convert HTML characters into entities
$headers = 'From: '. $mail_from. "\r\n";
mail($mail_to, $subject, $comments, $headers);
$response = '<h2>Thanks for contacting us, will get back to you soon</h2>';
}
}
It returns a slightly different value than eregi but if I'm reading your code correctly you should be able to use the strpos() function to determine if a substring exists in a string. Eregi ignores case so you might have to combine this with a strtolower($string) call too.
Something like this:
if (strpos("\r",strtolower($name)) || strpos("\n",strtolower($name)))

PHP email Piping get 'to' field

I am trying to use email piping with PHP.
I have it working except I can't get the 'To' field.
I am using the following PHP code:
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));
}
fclose($fd);
mail('email#example.com','From my email pipe!','"' . $to . '"');
?>
If I use a email address (for example: john#smith.com) and send a email to my email address that is forwarded to my PHP piping script (pipe.php) I want it to be able to get who the email was sent to.
For Example:
john#smith.com emails my forwarding email that goes to my PHP piping script (bob#example.com) I want it to return just the bob part only without the #example.com
What happens now is that it returns the whole email address such as "bob#example.com" and I want it to only return bob (without any talking marks).
I have tried using this:
$to = $to.split("#");
but I seem to get an error that says: split() expects at least 2 parameters.
That gets sent to the person who sent the email.
Does anyone know how to do this or know what I might be doing wrong?
This is my first time using Piping in PHP, so if I am doing something wrong please let me know.
Ended up using:
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));
list($toa, $tob) = explode('#', $to);
then changed the mail to:
mail('email#example.com','From my email pipe!','"' . $toa . '"');

send email to multiple recepients

I'm trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?
$elist = $database->getRows("SELECT * FROM `emails`");
if ($elist) {
foreach ($elist as $elist_result) {
$frm = 'rdsyh#gmail.com';
$sub = 'Weekly Work Report';
ob_start(); // start output buffering
include_once('mail_content.php');
$mail_body = ob_get_contents(); // get the contents from the buffer
ob_end_clean();
$to = $elist_result['email'];
$mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);
}
}
if ($mailstatus == 'ok') {
echo '<center><font color=red style="font-size:14px">Message has been sent Succesfully.....!</font></center><br>';
} else {
echo $mailstatus;
}
Well, there's a lot of abstraction here that we know nothing about from your code. Things to check:
Are you certain that your database query is returning all of the results you're looking for (is $elist populated properly)?
Are you certain that the query is returning data in the format that you're trying to access it in (is $to populated properly)?
Are you certain your l_mail() function is behaving (is it possible it exit's or otherwise terminates script execution in the middle of the first pass)?
Based on what I see here, if everything else was working properly, you should successfully be sending a bunch of emails, one to each email in your list.
Now, if instead you're trying to send a single email that is sent to all of the addresses at once, then you need to group the email addresses in the for loop and then run your mail function afterwards:
<?
$tos = array();
foreach ($elist as $elist_result) {
$tos[] = $elist_result['email'];
}
$frm = 'rdsyh#gmail.com';
$sub = 'Weekly Work Report';
ob_start(); // start output buffering
include_once('mail_content.php');
$mail_body = ob_get_contents(); // get the contents from the buffer
ob_end_clean();
$to = implode(', ', $tos);
$mailstatus = l_mail('', '', $to, $elist_result['firstname'] . ' ' . $elist_result['lastname'], $frm, 'HR', $sub, $mail_body);
?>
What does l_mail() do? If its a web service, then it might have limit for mass emails.

Mail function in PHP

I'm using PHP for sending an e-mail. The values in the e-mail are depending on the inputs of a form. But for some reason, the mail is suddenly not sending. It did before. What's wrong with my code?
Orders are placed correctly in the database, so no error there.
if ($order->addOrder($_DB)) {
$user = "SafetyCam.be";
$verzonden = FALSE;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$zip = $_POST['zip'];
$place = $_POST['place'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$twogig = $_POST['vn_qty'];
$fourgig = $_POST['ja_qty'];
$total = $_POST['total'];
$totaal = (($twogig * 50) + ($fourgig *80) + 2.5);
$headers = 'From: info#something.be';
$to = 'me#gmail.com';
$subject = 'Bevestiging bestelling';
$message = 'Hello $firstname,
You placed the following order on our website.
- $twogig x 2GB SafetyCams ordered
- $fourgig x 4GB SafetyCams ordered
+ shippingcosts (2,5 EUR)
The total cost for this order amounts to $totaal EUR.
Your products will be delivered as quickly as possible after receipt of your payment.
Please transfer the order amount of $totaal EUR into account X.
After payment, the products will be sent to the following address:
$firstname $lastname
$address
$zip $place
$country
We hope you will be very happy with your purchase.
Sincerely yours";
if (mail($to, $subject, $message, $headers)) {
$verzonden = TRUE;
$feedback = '<div class="feedbackheader">Thanks</div><br / >';
} else {
$verzonden = FALSE;
$feedback = '<div class="feedbackheader">Error!</div>'; }
}
else {
$feedback = '<div class="feedbackheader">Error!</div>';
}
}
Why do you open your mail $message with a single quote and end it in a double quote.
You should open and end with both double quotes, especially since you use PHP variables inside.
$message = 'Hello $firstname"; //Wrong
$message = "Hello $firstname"; // Works
You've opened the "message" string with an apostrophe ' but tried to close it with a quotation mark ". The SO syntax highlighter gives it away!
You have started your variable $message = 'Hello $firstname, with single quote and end it with double quote, what you need to do is just make
$message = "Hello $firstname
if you put it in single quote php wont scan your variable content for varible like $firstname
Your $message variable starts the string with a ' but ends it with a ", so all the code after it is included in the variable until another ' which happens when your defining $feedback.
Basically you are not closing the string, and therefore your entire code is being changed. If you are using color coding you should have seen this (I can see it from your question).
Also, if you are using single quotes, you cannot use inline variables.
$var = 1;
echo '$var'; // ouput: $var;
echo "$var"; // output: 1
You start your message-string with a single quote (') and try to end it with a double quote, thus your logic is parsed incorrectly.
I use SwiftMailer:
require_once('../lib/swiftMailer/lib/swift_required.php');
function sendEmail(){
//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n";
//Create a message
$message = Swift_Message::newInstance('Subject goes here')
->setFrom(array($email => "no-reply#yourdomain.com"))
->setTo(array($email => "$fname $lname"))
->setBody($body);
//Send the message
$result = $mailer->send($message);
}

Categories