dongxian8048 2012-10-01 14:44
浏览 39
已采纳

PHP - 基于数据库查询的电子邮件地址

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("(|
)(to:|from:|cc:|bcc:)",$body);
if ($email && $body && $valid && !$crack){
  if (mail($to,$messageSubject,$body,'From: '.$email."
")
      && mail($email,$confirmationSubject,$confirmationBody.$body,'From: '.$to."
")){
    $displayForm=false;
  • 写回答

1条回答 默认 最新

  • dongzhimin2231 2012-10-01 14:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了