Hey Guys,

I am writing this code to send out personalised emails to potential customers. And i have few questions about the code. This code is made from several tutorials and code examples. I have searched for stuff on it and theres plenty of articles about it but not what i want to do.
If anyone knows of one please throw us a link.

This has two pages the first is just a basic form to fill in the subject thats it really. And the second is below.

I'll reference the question next to the code its related to. :)

Q1. Is this line and Q2 related or should they be related?

Q2. What do these two lines do and what are they meant to do? They don't make sense to me but i've rechecked the tutorial and thats whats there? Along with the fact the varible $email isn't assigned to anything(Im gettin an erroe for it at the moment.)?

Q3. This bit of code is working but not correctly all i get when i view the emails (i am just testing by sending 4 emails at the moment) is the first name from the first person in the database?

<?php
// Check, if submitted.
//if($submit)
if(isset($_POST['submit'])){

// Get variables from POST method form.
$subject=$_POST['subject'];
//$note=$_POST['note'];

Q1. line below

$sender="***********"; // Your Email here.

echo "Email has been sent to:";

// Connect database
mysql_connect("localhost","root","");
mysql_select_db("******");

$rs=mysql_query("select * from mailList LIMIT 20 ");
// if (!$query) die(mysql_error());


// Do while loop to send email.
while($row=mysql_fetch_assoc($rs)){
$to=$row['email'];

Q2. lines below

$mail_from="From:$email n";
$mail_from .="Content-Type: text/html; charset=utf-8 n";

Q3.lines below

$note .= "<p>Hi ".$row['owner']." <br /><br />";
$note .="";


mail($to,$subject,$note,$mail_from);

// Show sent emails.
echo "$row[email]<br>";
}
}
else{

// Do following codes if not found "Submit" value.(Not submitted)
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<title>Email Form</title>
<body>
<form action="<? echo $PHP_SELF; ?>" method="post" name="form" id="form">
<table>
<tr>
<td align="right">Subject : </td>
<td><input name="email" type="text" id="email" /></td>
</tr>
<tr>
<td align="right" valign="top">Note : </td>
<td><textarea name="comment" cols="60" rows="5" id="comment"></textarea></td>
</tr>
</table>
<input type="submit" name="Submit" value="Send Email" />
</form>
</body>
</html>
<?php } ?>

Any help will be greatly appreciated.

Thanks in advance.

Recommended Answers

All 13 Replies

The PHP manual will answer most of your questions. This is what it says for mail function:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

$to - Receiver, or receivers of the mail
$subject - Subject of the email to be sent
$message - Message to be sent
$additional_headers (optional) -  String to be inserted at the end of the email header. This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). 

In the manual there are useful examples. In your case have a look at the example #2:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

So the answers to your questions are.

Q1: The $sender="***********"; code is meant for setting a sender envelope header which tells the server who is the sender. It should be used in $additional_parameters part using the -f switch something like:

mail($to, $subject, $message, $headers, "-f$sender");

Please consult the PHP manual for correct syntax.

Q2: The lines

$mail_from="From:$email n";
$mail_from .="Content-Type: text/html; charset=utf-8 n";

set the From: header and the Content-Type header (as part of additional headers). The From and the sender might be the same but this is not necessary. The From should be present otherwise the server might complain. The sender can be also set in php.ini (I think).

Q3: The code

$note .= "<p>Hi ".$row['owner']." <br /><br />";
$note .="";

Should give you an error sonce you are concatenating to a unndefined variable ($note has not ben declared yet). But if you uncomment line 8 in the first block of code:

//$note=$_POST['note'];

$note (the actual message) will be defined and values can be concatenated.

Q1. Is $sender="***********"; related to $mail_from="From:$email n"; or should it be ?? as $email isn't declared anywhere.

Q2. Are both lines need or could i get rid of one??

Q3. The note thats commented out is like that because it was orginally been sent from the previous page where the subject is also given thing is i don't want to do that. how would i declre it in that case??

Member Avatar for Zagga

Hi,
I think your confusion is due to the badly named $mail_from variable. This is NOT just for the senders (your) email address. It should really be called $headers as it contains various bits of header information (like the senders email address, the reply to address, the content type etc).

Q1. $sender is being defined but not actually used. Change $mail_from="From:$email n"; to $mail_from="From:$sender \n"; (notice the n is escaped).

Q2. Answered by the above.

Q3. If you don't want to pull the note from the form you can just set it manually

$note = "This is my email message";

The following reference code (as broj1 also included) may help:

$email_to = "The address you are sending the email to";
$email_subject = "The text that appears as the subject of the email";
$email_message = "The actual email message.  You can use HTML here if we set the content type in the email headers to text/html (like we do below)";
$email_from = "the senders email address"; // Note this is ONLY used in the headers, not the actual mail function
$email_headers = "MIME-Version: 1.0\r\nContent-type:text/html;charset=utf-8\r\nFrom: $email_from\r\nReply-To: $email_from\r\n";

mail($email_to, $email_subject, $email_message, $email_headers);

I think you should use an other script from php.net (-> mail() ).

OK thats all workin great last question :)
The while loop is grabbing the emails properly but not the owners names i keep gettin the name from the first row in the database in all the emails?

Dp you mean $row['owner'] value is always the same? Can you post the whole while loop.

You should initialize the $note variable each time in the loop:

$note = '';
$note .= "<p>Hi ".$row['owner']." <br /><br />";
$note .="";

or shorter

$note = "<p>Hi ".$row['owner']." <br /><br />";
$note .="";    

Yes Sorry $row['owner'] is always the same i put in your suggestions but its still not working the first name in the database keeps coming up. heres the loop with the up to date changes: `

$rs=mysql_query("select * from mailList LIMIT 20 ");
// if (!$query) die(mysql_error());

//$owner= mysql_query("select owner from mailList LIMIT 20");
// Do while loop to send email.
while($row=mysql_fetch_assoc($rs)){


    $email_to = $row['email'];
    $email_message = "";
    $email_message = "Hi ".$row['owner']."  ";
    $email_message = "";
    $email_from = "NoReply@xxxxxxxx.ie"; 
    $email_headers = "MIME-Version: 1.0\r\nContent-type:text/html;charset=utf-8\r\nFrom: $email_from\r\nReply-To: $email_from \r\n";


   mail(  $email_to,  $subject,  $email_message,  $email_headers);


// Show sent emails.
echo "$row[email]<br>";
}

I've also included the sql for the while loop aswell.

Member Avatar for Zagga

With the code above, I can't see why the name wouldn't update on each iteration of the while loop.
I can see that the email message will be blank though, as each time you have declared $email_message you overwrite the previous value. The last declaration for this is blank (remove lines 10 and 12 to fix this).
Add echo "$row['owner']<br>"; after line 21 (but before the close of the loop) and see what is being echoed.

Test this query in phpMyAdmin (or other mysql client you prefer to use):

select * from mailList LIMIT 20

Paste the output here.

Theres only four rows in the database nd when i run it, it all comes back fine O_o implemented all changes and no joy still nt workin...

Can you still post the output of the above query.

Theres only four rows in it nd when i run it, it all comes back fine O_o implemented all changes and no joy still nt workin...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.