Jump to content
Larry Ullman's Book Forums

Recommended Posts

I previously contacted the forum under the topic "Emailing to confirm a posting"
I decide to start this new  topic as the old topic would beconme too unwieldy with several pages of code.
1. The code given below posts a topic into a table in my forum. It works fine.
2. I tried to add an email-sender to the end of the code with mixed results.  
At first it did send me emails when registered members posted new topics, but now it does not work
When it did send an email, the email contained no subject or message, but it did give the name of the sender.
Now I can no  longer experiment because an email is never sent.
The login credentials are processed by a file named login_funtions.php, and a file named process_post.php.

I would be most grateful if you would explain where I have screwed up.
 

<!doctype html>
<html lang=en>
<head>
<title>The form for posting subjects and messages</title>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="forum.css">
<style type="text/css">
#tab-navigation ul { margin-left:85px; }
form { padding-left:215px; }
</style>
</head>
<body>
<div id='container'>
<?php
session_start() ;
// Redirect users if they are not logged in
if ( !isset( $_SESSION[ 'member_id' ] ) ) { 
require ( 'login_functions.php' ) ; 
load() ; 
}
include ( 'includes/header_post.php' ) ;
echo '<h2>Post a Quotation</h2>';
// DISPLAY THE FORM FIELDS
echo '<form action="process_post.php" method="post" accept-charset="utf-8">
<p>Choose the Subject: <select name="subject">
<option value="Comical Quotes">Comical Quotes</option>
<option value="Wise Quotes">Wise Quotes</option>
</select></p>
<p>Message:<br><textarea name="message" rows="5" cols="50"></textarea></p>
<p><input name="submit" type="submit" value="post"></p>
</form>';
include ( 'includes/footer.php' ) ;
//posting an entry into the database table automaticlally sends a message to the forum moderator 
$mailto = "moderator@myisp.co.uk" ;
$subject = "Message posted" ; 
$uname = ($_SESSION['uname']); 
$forum = ('subject') ;
$message = ('message') ;
$messageproper = 
          "------------------------------------------------------------\n" .
          "Name of sender: $uname\n" . 
          "Forum: $forum\n" . 
          		
          "------------------------- MESSAGE -------------------------\n\n" . 
          $message . 
          "\n\n------------------------------------------------------------\n" ; 
mail($mailto, $subject, $forum, $messageproper, "From: \"$uname\" " );
?>
</div>
</body>
</html>
Link to comment
Share on other sites

You do have some pitfalls here.

 

The last parameter you add ("From: $uname") does:

1.) Not exists. Optional headers, your $messageproper, is the last valid param you can specify.

2.) Is not a valid From address. According to RFC 2047, you need to specify it using one of the following formats:

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User <user@example.com>
  • User <user@example.com>, Another User <anotheruser@example.com>

Optional headers.

You cannot apply headers the way you are doing it. The "From:" header should be set either here or in your php.ini, and should only include valid headers such as "To:", "bbc:" and the likes. Because they are headers (what makes computers understand the content of the packages you send) you need to be very careful to follow formats. If the receiver can't understand headers, packages will be dropped...

 

Apply the content of both the $mailto and the $mailfrom (a variable I add below) in the specific format I specified above.

 

Try something like this:

$headers = 'To: '. $mailto . PHP_EOL;
$headers .= 'From: ' . $mailfrom . PHP_EOL;
// $header .= ... additional headers here...

The next one is line endings.

Instead of using LF, try doing CRLF endings instead. ("\r\n") However, some Linux servers will struggle with that, even if it's the RFC 2047 recommendation. The trick is to use the PHP constant PHP_EOL instead of setting your line endings manually. That will let your server apply the needed line endings automatically.

 

Weird code

Don't add unnecessary parenthesis to your strings or spaces before your ending semi-colons. No-one I've ever seen writes code like that, and it can potentially introduce bugs or parse errors. Only use parenthesis when it's necessary (casting) and remove them otherwise.

 

Hope that helps you out.

  • Upvote 1
Link to comment
Share on other sites

Many thanks Antonio for pointing me in the right direction. I new nothing about $headers because I was using manuals by Larry Ullman and others and they do not

mention $headers. I looked thorught 3 other manuals and eventually found a description of $headers in a book published in 2006. I then searched the internet and found

plenty of information there.

I almost missed the concatenation full stop just before the equals sign in your second example of $headers. I know now that it prevents the second statement from

overwriting the first, and it joins the two statements.

I also mistakenly thought that the 'From' referred to the sender's name, I now know it refers to the sender's email address.

In my case, I am sending an email to myself to alert me that a forum member has posted a message, therefore, the $mailto and the From are the same.

May I test your patience further and ask you to look over my suggested revision of the email code as follows:

$mailto = "me@myisp.co.uk";
$mailfrom = "me@myisp.co.uk";
$emailsubject = "Posting added";
$forum = "subject";
$message = "message";
$message = wordwrap($message, 70, "\r\n");
$uname = ($_SESSION['uname']);
$postedby = 'Posted by:' $uname\n;
$headers = 'To:'. $mailto . PHP_EOL;
$headers.= 'From:' . $mailfrom . PHP_EOL;

mail($emailsubject, $forum, $message, $postedby, $headers);

I suspect I have duplicated some items unnecessarily and I am unsure of where single or double quotation marks should be used.

I have taken careful note of your other comments about the code.

 

  • Upvote 1
Link to comment
Share on other sites

That is almost correct. The mail function takes a total of 5 params, where the last to is optional. It basically has this format:

mail ( $mailTo, $subject, $message, $headers, $params )

In your case, I would alter it to something like this:

// Set from address
$from = "me@myisp.co.uk";

// Assign subject
$user = isset($_SESSION['uaname']) ? $_SESSION['uaname'] : "";
$subject = "Posting added by " . $user;

// Build here message
$message = "very long message";
$message = wordwrap($message, 70, "\r\n", true);

// Create header
$headers = 'To:'. $mailto . PHP_EOL;
$headers.= 'From:' . $mailfrom . PHP_EOL;

// Finally, send mail
$status = mail($from, $subject, $message, $headers);

// Some simple debugging
echo $status ? "The Email was successfully sent" : "mail function failed";

Just build the subject and message string as you want, and you should be pretty much good to go.

 

Btw. Remember to check the PHP manual. You can find lots of information on mail() here: http://php.net/manual/en/function.mail.php

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...