Grab column from table and php mail?

Discussion in 'PHP' started by wvccboy, Mar 14, 2009.

  1. #1
    This should be simple but I'm overthinking this.

    I want to grab a column called Email in my table which looks like this:

    |-Name---------Email---------Group|
    |-Jim--------jim@joy.com-----Group|
    |-Me--------me@myself.net---Group|

    I simply need to grab the Email column and place it into an array which goes into a To: field of a simple php mail function like

    <?php
    
    $Name = "Da Duder"; //senders name
    $email = "email@adress.com"; //senders e-mail adress
    $to = "email@address.com";
    $to .= "2email@address.com";
     $mail_body = "The text for the mail..."; //mail body
    $subject = "Subject for reviever"; //subject
    $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
    
    mail($recipient, $subject, $mail_body, $header); //mail command :)
    ?>
    Code (markup):
    So the script should constantly add a new To field for each column it goes through.

    Ideas and code would be helpful.

    Thanks :eek:
     
    wvccboy, Mar 14, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Copy/paste example of your table code.
     
    steelaz, Mar 14, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    First off, you probably don't want to send this as "To:" - most user don't think too fondly of having their email-address sent to lots of strangers - therefore the following script uses BCC (Blind Carbon Copy) for the user's email-addresses.

    The only address showing up is the one "main" email-address, set in the script

    This is sort of a hacked together version of what I'm using on a page myself - I removed the verification, changed the language to English, and removed some small items here and there, so there might be errors in the script, but it should give you a start, at least.

    This sends out a HTML-mail, and therefore the CSS for the mail itself is added - this can of course be changed as you want.

    
    function mass_email()
    {
    global $ppm;
    $result_email=mysql_query("SELECT * FROM $database");
    
    $text_email=$_POST['email_text'];
    $subject_email=$_POST['email_subject'];
    
    if(!isset($_POST["email_subject"])) {
    	$subject_error = "";}
    elseif (empty($_POST["email_subject"])) {
    	$subject_error = "<p class='error'>You haven't written a subject</p>";}
    else { $subject_error = ""; }
    if(!isset($_POST["email_text"])) {
    	$text_error = "";}
    elseif (empty($_POST["email_text"])) {
    	$text_error = "<p class='error'>You haven't written a message</p>";}
    	else { $text_error = "";}
    $text_email = str_replace("\n","<br />", $text_email);
    }
    if ((isset($_POST["email_text"])) && (isset($_POST["email_subject"])) && $text_error == "" && $subject_error == "") { ?>
    <fieldset id='mass_email'><legend><a href="javascript:toggleLayer('mass_email_form');" title='The email that was sent'>The email that was sent</a></legend>
    		<p><label>Subject:</label><br />
    			<?php echo $subject_email; ?></p>
    		<p><label>text:</label><br />
    			<?php echo $text_email; ?></p>
    <?php
    			// this is the senders own email, given as the only "To:" email (We can add more if we want to)
    			$to  = 'mail@mail.mail' . ', '; // note the comma
    
    			// subject
    			$subject = "$subject_email";
    
    			// message
    			$message = "
    			<html>
    			<head>
    			  <title>$subject</title>
    			  <style type='text/css'>
    			  body {
    			  	font-family: sans-serif;
    			  	}
    			  .logo {
    			  	background: url(dragelogo.gif) no-repeat center;
    			  	height: 100px;
    			  	margin: auto;
    			  	text-align: center;
    			  	}
    			  fieldset {
    			  	border: 2px #000 ridge;
    			  	margin-top: 10px;
    			  	_margin-top: 20px;
    			  	padding: 10px;
    			  	background: #efefef;
    			  	_position: relative;
    			  	_margin-top:1em;
    			  	_padding-top:.75em;
    			  	}
    			  fieldset p {
    			  	_margin-top: 20px;
    			  	}
    			  #input {
    			  	width: 700px;
    			  	margin: auto;
    			  	}
    			  #input .formlook, #search .formlook, #search_page .formlook {
    			  	color: #333;
    			  	border: 2px solid #000;
    			  	padding: 2px;
    			  	}
    			  input:focus,select:focus,textarea:focus  {
    			  	outline: 2px solid #2f5376;
    			  	}
    			  legend {
    			  	background: #2f5376;
    			  	color: #fff;
    			  	font-weight: bold;
    			  	padding: 2px 5px;
    			  	border: 2px #000 ridge;
    			  	_position:absolute;
    			  	_top: -.9em;
    			  	_left: .7em;
    			  	}
    			  label {
    			  	color: #2f5376;
    			  	font-weight: bold;
    			  	}
    			  .gopher {
    			  	color: #fff;
    			  	background-color: green;
    			  	padding: 0px 5px;
    			  	}
    			  .important {
    			  	color: #f00;
    			  	font-size: 120%;
    			  	}
    			  .error {
    			  	color: #fff;
    			  	background-color: #f00;
    			  	padding: 0px 5px;
    			  	}
    			  dl {
    			  	color: #2f5376;
    			  	}
    			  dt {
    			  	font-weight: bold;
    			  	padding-top: 5px;
    			  	}
    			  dl dl {
    			  	color: #000;
    			  	margin-left: 50px;
    			  	}
    			  dd {
    			  	font-weight: normal;
    			  	padding: 2px 5px;
    			}
    			</style>
    			</head>
    			<body>
    				<p>$text_email</p>
    				<p>Regards<br />
    				Sender of email</p>
    			</body>
    			</html>
    			";
    			// To send HTML mail, the Content-type header must be set
    			$headers  = 'MIME-Version: 1.0' . "\r\n";
    			$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    
    			// Additional headers
    			$headers .= "From: $email" . "\r\n";
    			while($email=mysql_fetch_array($result_email)) {
    			$headers .= "Bcc: $email[mdl_e_mail]," . "\r\n"; }
    
    			// Mail it
    			mail($to, $subject, $message, $headers);
    ?>
    <?php } else { ?>
    <fieldset id='mass_email'><legend><a href="javascript:toggleLayer('mass_email_form');" title='Sends out email to all members with a registered email-address'>Sends out email to all members with a registered email-address</a></legend>
    	<form id='mass_email_form' method='post' action='<?php echo "$domain"; ?>/massemail.php'>
    		<p class='warning'>Sends out email to all members with a registered email-address!</p>
    		<p><label>Subject:</label><br />
    				<input class='formlook' type='text' name='email_subject' size='65' value='<?php echo $subject_email; ?>' id='email_subject' /></p>
    						<?php echo $subject_error; ?>
    		<p><label>text:</label><br />
    			<textarea name='email_text' class='formlook' rows='9' cols='60' ><?php echo $text_email; ?></textarea></p>
    						<?php echo $text_error; ?>
    		<p><input class='button' type='submit' value='Send email' /></p>
    </form>
    <?php }}
    
    PHP:
    Rep is always appreciated ;)
     
    PoPSiCLe, Mar 14, 2009 IP
    wvccboy likes this.
  4. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #4
    You mean just something like this:

    Ok the code from Popsicle works except for the mass email. It sends to the first email address in the DB and not all of them.
    Thanks, and +rep.
     
    wvccboy, Mar 14, 2009 IP