I have been researching this but cant sem to get an answer.
I have a form that is sending the info to my database, I also want the info to go to email addresses which are stored in a column in my table in the database. Heres the code, I was just playing around to see if I could do a SELECT query but it wouldnt work, also Iknow that sending the email works when I manually ut an address into my "setTo"
require 'mail/class.simple_mail.php';
$mailer = new SimpleMail();
$comments = $_REQUEST['comments'];
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$place = $_REQUEST['place'];
$email = "SELECT email FROM bridesmaids";
$message = "<strong>This is an email from the bride to you the bridesmaids, here is the information on the next dress fitting<br />".$date." ".$time." ".$place." ".$comments." </strong>";
$send = $mailer->setTo('$email', 'Your Email')
->setSubject('Dress Fitting')
->setFrom('donaghy-e5#email.ulster.ac.uk', 'Domain.com')
->addMailHeader('Reply-To', 'donaghy-e5#email.ulster.ac.uk', 'Domain.com')
->addMailHeader('Cc', 'donaghy-e5#email.ulster.ac.uk', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Your Email has been sent to your bridesmaids' : 'Could not send email';
If you haven't created an SQL connection, do this first then:
1: Select your emails from your bridesmaids table - then return the results into an array (PDO::FETCH_ASSOC or fetch_array(MYSQLI_ASSOC)) and bind this array to a variable e.g. $mailingList.
2: Wrap your mailer code in a foreach loop (http://uk3.php.net/manual/en/control-structures.foreach.php) an example would be:
foreach($mailingList as $value) {
// your mailer code here, replace $email with $value
$send = $mailer->setTo($value, 'Your Email')
// $value will update for each iteration with your emails
}
3: Ensure any code or variables which do not need to be repeated are outside of the loop.
You don't query the database for the actual e-mail adresses. You just have s string that contains an SQL Query but you never actually connect to a database and fetch data using a query.
$email = "SELECT email FROM bridesmaids";
This should become something like this:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($result = $mysqli->query("SELECT email FROM bridesmaids")) {
printf("Select returned %d rows.\n", $result->num_rows);
$result->close();
}
Related
I need help for this PHP script:
<?php
$link = mysql_connect('localhost', 'Username', 'PW');
$sql = "SELECT Driver, Mail FROM mytable";
$query = mysql_query($sql);
$emailBody="";
$Subject="test";
$to ="xxx";
while($row = mysql_fetch_assoc($query))
{
$emailBody .= "Driver: ".$row['Driver']."\n";
}
$to = str_replace('xxx', $row['Mail'], $to);
mail($to, $Subject, $emailBody);
I tried many things:
mail($to, $Subject, $emailBody);
mail($row['Mail'], $Subject, $emailBody);
I cannot set the field value of row['Mail'] as the receiver address.
The field values have been tested with "mymail#mail.com" and 'mymail#mail.com' and mymail#mail.com .
The replace function was just another trial.
$to =str_replace('xxx',$row['Mail'],$to);
Nothing works except hardcoding for test only..
mail("mymail#mail.com", $Subject, $emailBody);
Thanks!
$row only exists inside your loop (i.e. inside the { and }). But your $to line exists outside the loop, therefore it cannot access any values from $row.
If, as you mention in the comments, you wish to generate a separate email for each row of your query results then
a) you need to move the code which sets the recipient and sends the mail inside the loop so it can access the row data, and execute once per row
and
b) you need to not concatenate the body with data from every row, otherwise every email after the first will contain details from all the previous rows as well as the current one.
This should work better:
$link = mysql_connect('localhost', 'Username', 'PW');
$sql = "SELECT Driver, Mail FROM mytable";
$query = mysql_query($sql);
$Subject="test";
while($row = mysql_fetch_assoc($query))
{
$emailBody = "Driver: ".$row['Driver']."\n";
mail($row['Mail'], $Subject, $emailBody);
}
P.S. As I mentioned in the comments, you need to urgently replace the obsolete mysql_ calls with mysqli or PDO, and urgently upgrade to a supported version of PHP. I'd also recommend using a library like PHPMailer to handle your email sending - it's easier to work with than mail() and is more likely to generate valid emails which won't get accidentally blocked as spam etc. It can also support SMTP where mail() only supports local mailservers via sendmail.
I am using the following code:
$sql2 = "SELECT * from contacts where company_sequence = '".$customersequence."' and receive_accountsemails = 'yes' ";
$rs2 = mysql_query($sql2,$conn) or die(mysql_error());
while($result2=mysql_fetch_array($rs2))
{
$emails_list[] = $result2["email"];
}
This puts all email addresses from the contacts table into an array and then I send the emails to implode(',',$emails_list) so it separates each email address with a comma.
What is the best way to just get one email address into a variable on its own so I can insert each email sent into a different table? When I put sentto it wont do the whole array of emails.
Best way is to use another foreach loop:
foreach($emails_list as $email)
{
$user_email = $email;
// Then you can send email in loops for each email. Target email is stored in $user_email or just $email
}
Simply do not glue them all together with joinbut traverse them with
foreach ( $emails_list as $mail ) {
// do something with each individual $mail
}
i am trying to create a reply to an ad form where a user clicks an ad and can send an email, however i do not want the users to actually see which address they are sending it to but instead the email address can be taken from a database based on the username of the ad
i have tried this code but cant seem to get it to work, any help appreciated!
$username = $_SESSION['username'];
$sql = "SELECT * FROM user WHERE username=:username";
$q = $conn->prepare($sql);
$q->bindParam(':username', $username, PDO::PARAM_STR);
$q->execute();
$row = $q->fetch(PDO::FETCH_ASSOC);
$email1 = $row['email'];
$to='$email1';
$messageSubject='Message subject';
$confirmationSubject='Confirmation message subject';
$confirmationBody="Confirmation message body";
$email='';
$body='';
$displayForm=true;
if ($_POST){
$email=stripslashes($_POST['email']);
$body=stripslashes($_POST['body']);
// validate e-mail address
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+#([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
$crack=eregi("(\r|\n)(to:|from:|cc:|bcc:)",$body);
if ($email && $body && $valid && !$crack){
if (mail($to,$messageSubject,$body,'From: '.$email."\r\n")
&& mail($email,$confirmationSubject,$confirmationBody.$body,'From: '.$to."\r\n")){
$displayForm=false;
Don't use the ereg functions. They're deprecated and have serious issues. Use preg instead.
To answer your question, this should be simply
$to = $email1;
or better yet, skip that stage and do
$email1 = $row['email1'];
With the ' single quotes, you're forcing PHP to treat $email1 as a string, not as a variable. so you're trying to send to an address named $email1, not the contents of the $email1 variable.
How can I make it so when a user email's to my email address their email text/information is written into a mysql table? So basically extract the contents of a new email and write them into mysql table.
I tried this but I get nothing:
<?php
$imap = imap_open("{gmail.com}", "username", "password");
if( $imap ) {
//Check no.of.msgs
$num = imap_num_msg($imap)
//if there is a message in your inbox
if( $num >0 ) {
//read that mail recently arrived
echo imap_qprint(imap_body($imap, $num));
}
//close the stream
imap_close($imap);
}
?>
We are using an exchange server..I am a coop student so I am not really advanced at this.
I tried this as a test to see if it works, logging in gmail to read email. It didnt work.
<?php
// connect to the mailbox
$m_mail = imap_open("{mail.https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2}INBOX", "username", "password");
//get all messages
$m_search=imap_search ($m_mail, 'ALL ');
// Order results starting from newest message
rsort($m_search);
//loop through and do what's necessary
foreach ($m_search as $onem) {
//get imap header info for obj thang
$headers = imap_headerinfo($m_mail, $onem);
$head = imap_fetchheader($m_mail, $headers->Msgno);
$body = imap_body($m_mail, $headers->Msgno, FT_INTERNAL );
echo $body;
}
//purge messages (if necessary)
imap_expunge($m_mail);
//close mailbox
imap_close($m_mail);
?>
use IMAP functions for that. Set up another email account if necessary. Here's the example code:
// connect to the mailbox
$m_mail = imap_open("{mail.YOURHOST.com:993/imap/ssl/novalidate-cert}INBOX", "address#YOURHOST.com", "YOURPASSWORD");
//get all messages
$m_search=imap_search ($m_mail, 'ALL ');
// Order results starting from newest message
rsort($m_search);
//loop through and do what's necessary
foreach ($m_search as $onem) {
//get imap header info for obj thang
$headers = imap_headerinfo($m_mail, $onem);
$head = imap_fetchheader($m_mail, $headers->Msgno);
$body = imap_body($m_mail, $headers->Msgno, FT_INTERNAL );
//
DO WHAT YOU NEED TO DO HERE - insert to the database, etc
}
//purge messages (if necessary)
imap_expunge($m_mail);
//close mailbox
imap_close($m_mail);
read your mailbox via pop3 or imap and add the new emails to your database
Take a look a this class: http://php.net/manual/en/book.imap.php
you could read the pop3/imap in some intervals.
other method would be to redirect your email to a php script. I don't know which email system you are using on your server but here is an example with postfix.
the pop3/imap method is easier and you don't need to edit your server configs but the second method is faster because your script will start as it receives an email.
Right now I'm trying to make an email notification whenever somebody submits a comment going into a database. I'm using mysql_real_escape_string() to protect against injections and I'm trying to strip the slashses off the name, email, and comment when they are sent in the email notification. The database entry code is commented out, as I don't want there to be an entry made every time I test the email system.
The email is sent just fine, however stripslashes() is not working properly. For example, if the message is "Can't wait!" the email will have "Comment: Can\'t wait!". This is strange to me because when I have the comments displayed on the page, stripslashses works fine.
Is this because I'm not applying stripslashes() to data not coming directly out of the database?
$li = mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect");
mysql_select_db($dbDatabase, $li) or die ("could not select DB");
$name = mysql_real_escape_string($HTTP_POST_VARS["name"]);
$email = mysql_real_escape_string($HTTP_POST_VARS["email"]);
$comment = mysql_real_escape_string($HTTP_POST_VARS["comment"]);
$date = Date("Y-m-d h:i:s");
/*
$gb_query = "insert into entries
values(0, '$name', '$email', '$comment', '$date')";
mysql_query($gb_query);
$res = mysql_affected_rows();
// See if insert was successful or not
if($res > 0) {
$ret_str="Your guestbook entry was successfully added!";*/
$name2=stripslashes($name);
$email2=stripslashes($email);
$comment2=stipslashes($comment);
$subject = 'New Guestbook Entry Has been Added!';
$message = "Name: $name2\nEmail: $email2\nComment: $comment2";
$to = "chetg329#yahoo.com";
mail($to, $subject, $message);
//header('Location: guestbook.php?ps=1');
//exit(); // End the request
/*
} else {
$ret_str = "There was a problem with your guestbook entry. Please try again.";
}
// Append success/failure message
$gb_str .= "<span class=\"ret\">$ret_str</span><BR>";*/
mysql_close();
My guess is that the problem lies within magic_quotes. I would take a look at your php.ini and see if you can turn it off (pending you are not relying on it to prevent SQL Injection). If you cannot access the php.ini to turn it off PHP provides a method for you to test it and act accordingly (IE stripslashes on the data before escaping it with mysql_real_escape_string ) It is called get_magic_quotes_gpc
Alternatively, why are you using mysql_real_escape_string on data that is not being inserted into the database? If you do insert it later on, then move those calls till after the mail call.