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

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.

Related

send html in an email via php, can't seem to do it

I have a list of subscribers who I am trying to send a HTML type email to (essentially a news letter). But currently, it is just sending plainly and I can't seem to figure how to make it look better.
I've searched through other questions but can't seem to apply the answers to my own code, so could someone help? Code is shown below:
<?php
$user = "example";
$password = "example";
$host = "example";
$dbase = "example";
$table = "example";
$from= 'example';//specify here the address that you want email to be sent from
$subject= $_POST['subject'];
$body= $_POST['body'];
// Connection to DBase
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
$query= "SELECT * FROM $table";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];
$msg= "Dear $firstname $lastname,\n$body";
mail($email, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
mysqli_close($dbc);
?>
So I have a message box where I can type a message but how would I make that message box, html style? So I can add in H2 etc tags? Or just make the email like a html newsletter
I have put example in on purpose
You need to send headers with html declared: example
$to = 'person#example.com';
$subject = 'Your subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: you#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = // your html code here
mail($to, $subject, $message, $headers);
For that you need to set your headers as text/html
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and pass that to your mail() function.
Something like this..
mail($to, $subject, $msg, $headers);
Your Modified Code
$msg= "<h3>Dear $firstname $lastname</h3>,<br><br><b>$body</b>"; //<-- Added some basic formatting
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Yourname <'.$from.'>' . "\r\n";
mail($to, $subject, $msg, $headers);
Refer : PHP Manual
Use PHP Mailer Class instead of mail()
$mail = new PHPMailer();// defaults to using php "mail()"
$mail->SetFrom("frommail");
$mailAdmin->AddAddress("tomail");
$mailAdmin->Subject = $subject;
$mailAdmin->MsgHTML($message);
$mailAdmin->IsHTML(true);
$mailAdmin->Send();
You just need to include a class file phpmailer class file wich can be downloaded online.

Button Link inside php mail message

I am trying to send an automated email which has a button to download a report; the problem I have is that I can not seem to find away for the php link to show as a button or even a link, it just prints out the code.
My code is below; any suggestions would be appreciated. Thanks
$to = "$email";
$subject = "Alert Report";
$message = "
<a href=\"http://www.mywebsite.com/test.php?id={$id}\" class='button'>Download Report</a>
";
$from = "My Website";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
You need to add some other header parameter:
$to = "$email";
$subject = "Alert Report";
$message = "
<a href=\"http://www.mywebsite.com/test.php?id={$id}\" class='button'>Download Report</a>
";
$headers = "From: $from\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
Put it inside html tags
//begin of HTML message
$message = "
<html>
<body >
<input type=\"button\" value=\"Download Report\" onclick=\"location.href='your site with param';\">
</body>
</html>
";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
As pointed in the comment By Fred , avoid using external css. Use inline instead. There are few bolgs available where you can learn about Do's Don't for email css
You'll need to use an image hosted on your site. Simply point a link to the report. The CSS is useless in the email since it's not defined.
i.e.
<img src="link_to_image_source" />

$_POST a input value in two parts (explode?)

I'm really new to PHP, so this is probably a pretty dumb question.
I'm using PHP to submit an email form, and would like the email to contain the values of some of the form's inputs. Here's a stripped down version:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
//here are the values that the email will send me
"<p>".$_POST('some-name')."</p>
<p>".$_POST('some-name')."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>
The value of input some-name is, say, 10-widgets posted from the form.
Here's the question: instead of listing "10-widgets" twice (as the above code will do), how do I list the first part in the first <p> (so it would be "10") and the second part in the second <p> (so it would be "widgets")?
Something like the following seems promising:
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
and then, somewhere, $_POST($partVal[0]); and $_POST($partVal[1]);
But I don't know where this should take place, and anywhere I put it seems to make the whole thing break.
Thanks for your help.
First, you should access $_POST with brackets, like $_POST['some-name']. explode returns an array. So in your example, explode('-', '10-widgets')[0] will return '10' and explode('-', '10-widgets')[1] will return 'widgets'
So your code will be something like this:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$parts = explode('-', $_POST['some-name']);
//here are the values that the email will send me
$message =
"<p>".$parts[0]."</p>
<p>".$parts[1]."</p>";
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
}
?>
I know you're looking for functionality at the moment but keep in mind that if you're splitting a string by a "-" it'd be really easy for a script kiddie (or even a well intentioned person for that matter) to use a hyphenated word and your script will break.
Just a heads up :D
Try that
$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);
Then into your email:
$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
//here are the values that the email will send me
"<p>".$partVal[0]."</p>
<p>".$partVal[1]."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>

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.

Any suggestions on how to trouble shoot internal server error for an almost identical script?

On my site I have a contactUser.php that sends mail and a replyMail.php. I have put PHPMail on my contact user to send email notifications which is working great. When I put the same code chunk on my replyMail (which mail goes to the same place as the contact user mail,the inbox) that shares the same variables its giving me a internal server error. I have tried different GET vars as well as echoed die($vars) and echoed queries. Any ideas what the problem might be. Here is my code:
$prof = new User($_GET['id']);
$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 sent you a message";
$message = "$auth->first_name $auth->last_name sent you a message:<br /> <br /> <a href='http://www.blah.org/Inbox.php'>Click here to view</a><br /><br /> The Team";
$from = "blah<noreply#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);
I really suggest wrapping it up into a function. From what you've posted, I get the sense that you have all the code in the global namespace, in which case any number of auto-prepends or includes could easily be messing with variables.
Something like:
// Function that accepts a user object and a database connection, then sends a notice email.
function email_message_notice($prof, $connection){
$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 sent you a message";
$message = "$auth->first_name $auth->last_name sent you a message:<br /> <br /> <a href='http://www.blah.org/Inbox.php'>Click here to view</a><br /><br /> The Team";
$from = "blah<noreply#blah.org>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:$from";
$res = mail($to, $subject, $message, $headers);
return $res;
}
$prof = new User($_GET['id']);
$sent = email_message_notice($prof, $connection);
Since there's a database involved, this is untested code, so you'll want to test that it works and want to debug any further issues yourself, but otherwise it should be a good way to compartmentalize and share the code. If you're doing exactly the same thing in both scripts, you can just include the email_user function in a separate include.
Look into the server log. Should tell you what is wrong. Maybe file permissions.
Is that the whole script? I can't see anything there that could cause a 500 error. Can you post the whole page or a link to the source code of the whole page?
Is this a part of some loop or mass mailing that gets called repeatedly?
Is there no way to get hold of the Apache error logs? Only they will give you a definite answer what goes wrong.

Categories