php newsletter script duplicating body content - php

just done a little bit of code to send out a newsletter based on a sql table.
first one with 70/80 subscribers went off fine, now when i've moved to the second one that has around 250, the body_message of the email is repeated inside the email equally to the number of people on the mailing list, in this case I was sending emails with 250 duplicates of the content inside...
not sure whats wrong with the code, have stripped it down as much as i could and was wondering if someone could talk a look and hopefully point out the issue
<?php
$i=1;
if (isset($_POST['submit_btn'])) {
connect_newsletter();
$result = mysql_query("SELECT id, mail FROM test") or die('Could not connect. ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
$email = $row['mail'];
$nid = $row['id'];
$ip=$_SERVER['REMOTE_ADDR'];
$ref="http://www.domain.co.uk";
$body_message ='newsletter html code';
$y_email="noreply#domain.co.uk";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers4=$y_email;
$headers .="Reply-to: $headers4\n";
$headers .= "From: $headers4\n";
$headers .= "Errors-to: $headers4\n";
$subject="subject";
mail($email,$subject,$body_message,$headers);
echo $i." sent to ".$email;
echo "<br>";
$i++;
}
}
?>

Watching your code that is not possibile because in the loop you safely reset the value of $body and $subject
The problem could be somewhere else. Check your sendmail log

Related

php mailer does not send degree symbol

OK, I have a php mailer program that works great sending html emails with total success UNLESS they contain a degree symbol or ampersand.
I imagine that it's an encoding issue but no-matter what I try the output email stops after (and including) the degree symbol.
Can I have another pair of eyes please. Here is the relevant php code:
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charSet=utf-8" . "\n";
$headers .= "bcc: $emailList\r\n";
$headers .= "From: " . $data1. "<" . "xxx#gmail.com".">\r\n";
// next include a reply to
$headers .= "Reply-To: " . $data7 . "\r\n";
// often email servers won't allow emails to be sent to
// domains other than their own. The return path here will
// often lift that restriction so, for instance, you could send
// email to a hotmail account. (hosting provider settings may vary)
// technically bounced email is supposed to go to the return-path email
$headers .= "Return-path: " . $data7. "\r\n";
// now we can add the content of the message to a body variable
$message = "<!DOCTYPE html>";
$message .= "<html><head><title>Online test - ".$data8."</title></head><body>";
$message .= "<p style='font-family:verdana;font-size:12px;color:blue'>".$data1.", you have attempted ".$data3. " questions";
$message .= " in ".$data4." minutes and ".$data5." seconds";
$message .= " and scored a total of ".$data2.".</p>";
$message .= " <p style='font-family:verdana;font-size:12px;color:blue'>Final percentage = ".$data6."%</p>\r\n\r\n";
$message .= "<p style='font-family:verdana;font-size:12px;color:blue'>The following questions were answered incorrectly: </p><hr /><ul>";
foreach($myWrongArray as $my_Array){
$message .= "\r\n\r\n<li style='font-family:verdana;font-size:12px;color:teal'>".$my_Array."</li>";
}
//$message = strip_tags($message);
$message .= "</ul><hr /></body></html>";
// finally, send the email
mail($sendTo, $subject, $message, $headers);

mail function in php not sending headers

I brought a shared sever, and got a little storage space. When i run my web site, the mail function works perfectly. Mail has been sent with subject and message to all receivers. But the mail which is sent mentioned "From : ". Headers what i included are not working.
I want to send with my headers Eg:Form: mymail#mysite.com . How can i do it?
<?php
$output="";
if(isset($_POST['submit']) && !empty($_POST['message']))
{
$sub=$_POST['subject'];
$msg=$_POST['message'];
$sql = "select email from login where status='client'";
$query = mysqli_query($con,$sql);
$to=array();
while($row = mysqli_fetch_array($query))
{
$to[] = $row['email'];
$parts = implode(',',$to);
$headers = 'From: mymail#mysiet.com;';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$subject = $sub;
$content = $msg;
}
if(mail($parts,$subject,$content,$headers))
{
$output="mail sent";
}
else
{
$output="Error in connection, Mail could not be sent. Please try again";
}
}
?>
You need to manually specify the headers, this is the function I use and it delivers to pretty much every provider beautifully. The headers you want are From: and Reply-To: and I also advise the X-Mailer follows what is seen here, I found it years ago via SO as well.
<?php
$to = "user#anotherdomain.com";
$headers = 'From: My Name <email#domain.com.au>' . "\r\n" .
'Reply-To: My Name <email2#domain.com.au>' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-type: text/html; charset=iso-8859-1';
$subject = "My subject line here";
$message = 'Message HTML and Content Here.';
if(mail($to, $subject, $message, $headers)){
// Success Here
}else{
// Failed Here
}
?>
I am not 100% certain it will work for you as I am on a dedicated machine, having said that - I do use it on multiple domains and have never messed with my Apache or php configuration in relation to mail settings.
Read More about the Mail function in PHP: http://php.net/manual/en/function.mail.php
You can try below code, just ripped from php.net
// 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";
Off topic but good to use PHPMailer.

send mail by bbc not working

hay
i used this code
$to = "mial#live.com,mail#yahoo.com";
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
$headers .= 'Bcc: {$to}' . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo 'ok';
}
but see what is happend
every user see the full list of the users
alt text http://img694.imageshack.us/img694/1289/21811933.gif
Your call to mail is passing the $to as the to parameter meaning those emails will be be in the to header try passing an empty string instead. You are passing the info into the bcc header so the email should still get to them that way.
That is because you have put all the users in the "to" line. You are also passing them into the "bcc" line too so just doing this may help you but as far as I know you need at least one address in the to line (although this may not be the case). It'll look pretty strange for each person doing it that way though.
The best way to avoid these issues would be to send the email multiple times, once to each user. To modify your code example to do this, I'd do something like the following:
$toAddresses = array("mial#live.com", "mail#yahoo.com");
$subject = "Mini-mass Emailer";
$message = "<a href='#'>Hello World</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Name <me#mydomain.com>' . "\r\n";
foreach ($toAddresses as $to) {
if(mail($to, $subject, $message, $headers)){
echo "OK - sent message to {$to}";
}
}
The easiest way is to take this Mail-Class of phpguru.org:
http://www.phpguru.org/static/htmlMimeMail5
There you can specify with setBcc() the addresses which should be "blind", it's pretty easy and works well. I use this class in every project.
Best Regards.

How can I get phpmail to send to the right user and also limit the ammount of emails for certain users? Part 2

Thanks to Anthony's advice I've tried to simplify my logic and syntax of the goal I am trying to achieve which is when a blog is written, the AUTHOR gets notified by php mail that someone other than himself comments. When the AUTHOR comments everyone else who commented except him gets a different email. When another user comments, the AUTHOR gets notified as stated above, but everyone else who has commented gets an email, the same one that the AUTHOR sends OUT when he comments on his OWN blog and yes I'm STILL a newbie:
if(isset($_POST['commentBlogSubmit']) && $auth) {
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "$auth->first_name $auth->last_name left you a blog comment";
$message = "$auth->first_name $auth->last_name left you new blog comment:<br /> <br /> <a href='BlogProfile.php?id=" . $blog->id . "'>Click here to view</a><br /><br />";
$from = "<noreply#site.com>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:$from";
mail($to, $subject, $message, $headers);
if($blog->author != $poster->id) {
$query = "SELECT * FROM `BlogComments` WHERE `blogID` = '" .$blog->id. "'";
$request = mysql_query($query,$connection);
while($result = mysql_fetch_array($request)) {
$emailPoster = ($result['userID']);
$to = $emailPoster;
$subject = "$auth->first_name $auth->last_name Commented";
$message = "$auth->first_name $auth->last_name commented on the blog $blog->title :<br /> <br /> <a href='BlogProfile.php?id=" . $blog->id . "'>Click here to view</a><br /><br />";
$from = "<noprely#site.com>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:$from";
mail($to, $subject, $message, $headers);
}
Based on what you've posted, I would start with confirming that the second query is returning results. And second, how many. Instead of emailing the users, have it output to screen. Like:
while($result = mysql_fetch_array($request)) {
echo $result['userID']."\n";
}
Maybe add print_r($result) to confirm the keys are right, etc.
Second, if it's looping right, and the values are right, then I'm left thinking the issue is that it's not following the logic you want, that is the conditions for when to email and who.
Frankly, it seemed a bit confusing when you wrote it out. Maybe writing it in pseudo-code might help you see where the missing piece is.
If it was me, I'd make the rule based on who was posting. If there is a post, email everyone but the poster. That way, if it's the blogger, he won't get emails on his own posts but everyone else will. If it's not the blogger, he'll get the email, because his user ID didn't match that of the commenter.
Doing it that way eliminates a lot of rules based on who's who, and just makes it one simple rule.

Why are my links not showing up via php email function?

I am trying to send notification emails(which is working fine) but have added the html headers to try to send links etc...for some reason nothing is showing up at all, just blank space where the desired links are supposed to be. Here is my code:
if(isset($_POST['commentBlogSubmit']) && $auth) {
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "Someone sent you left you a comment";
$message = "You have a new blog comment <br />".
" <a href='http:www.blah.org/indexNew.php'></a>";
$from = "info#blah.org";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
mail($to, $subject, $message, $headers);
}
Perhaps because you have no text inside the link tag?
Because the PHP email function generally sends plain text.
Rather than trying to do this yourself, you should probably use Mail_Mime
Also, though your headers are probably correct, you have nothing between the <a> and </a> tags.

Categories