duannao3819 2011-09-28 15:25
浏览 40
已采纳

如何在使用邮件函数PHP时打印来自sql db的变量

I have a mail function that sends out an email with content from an sql db.

The message that will be included in the email will be taken from the sql db. The message in the sql db has variables in it.....

example:

In the database:

Hey $name, $owner_name here

Everything works fine, except i cant get PHP to print out that var when it sends the email.

So the final email that goes out is:

Hey $name, $owner_name here

When it should be:

Hey BOB, Johnny here

Here's the mail function code;

get_email_settings($db_host, $db_user, $db_pswrd, $db_name, $email, $pro_msg,$pro_subject);



mail($email_usr, $pro_subject, $pro_msg, "From: $my_email"); 

Now I've also tried: (NOTE: the double quotes)

mail($email_usr, "$pro_subject", "$pro_msg", "From: $my_email");

And I've also tried inserting the content into the DB with double quotes so: "Hey $name, $owner_name here" instead of Hey $name, $owner_name here...

Both do the same thing...

Any Ideas?

  • 写回答

6条回答 默认 最新

  • dongzi1959 2011-09-29 07:01
    关注

    OK, I got it working.

    For anyone else who has this issue, and wants a slightly more clearer answer, here it is:

    All I done was a multiple str_replace using arrays.

    I'm feeling generous, so I'll go into some detail for you...

    example of an str_replace:

    $my_text = "my name is [NAME] and my email is [EMAIL]"
    
    $find = array('[NAME]',[EMAIL]);
    
    $and_replace = array('BOB', 'BOB@BOB.com');
    
    // then I just run the str_replace into a variable:
    
    $final_text = str_replace($find, $and_replace, $my_text);
    

    and the final text would be "my name is BOB and my email is BOB@BOB.com"

    so as you can see, str_replace searched in the variable $my_text and looked for [NAME] and [EMAIL]. It then replaced them with either BOB (for [NAME]) and BOB@BOB.com (for [EMAIL]). BUT when you do this, make sure the replace variable ($and_replace) has the data in the right order... for example, if i did this $and_replace = array('BOB@BOB.com','BOB'); instead of this $and_replace = array('BOB', 'BOB@BOB.com'); then it would replace [NAME] with BOB@BOB.com and [EMAIL] with BOB. So make sure you get the right order....

    So in my case, using str_replace with a string coming from an SQL db:

    It's piratically identical to the example i wrote above, instead you just don't create the string variable ($my_text). You just use the variable that has the db content in it (in my case $pro_msg)

    so:

    $find = array('$name','$owner_name');//<< As some of the other guys said, you should use something like [NAME] instead of $name....
    
    $and_replace = array("$name", "$owner"); //<<<**NOTE: IF REPLACING WITH OTHER VARIABLES, USE DOUBLE QUOTES**
    
    
    $final_text = str_replace($find, $and_replace, $pro_msg);
    

    So it went into $pro_msg (which contains a string from my sql db) and searched for $name and $owner_name and then replaced it with the appropriate...

    ready for mailing...

    hope that helped someone out there...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据