dppi5167 2016-05-29 13:54
浏览 29
已采纳

从php页面发送电子邮件

Im trying to get an email to send from my php page to an email address that is set.

The email is being sent and received at the expected email address. But the contents within the email are not as expected. I got the code from watching a tutorial on youtube.

However when looking at the email is displays exactly as the code below, doesn't give me the values behind the variables e.g $name shows up in the email as $name

Any ideas?

The code im using is;

<?php 

require_once 'header.php';

$to = 'emailaddress ';
$subject = 'CSG';

$name = $_POST['name'];
$email = $_POST['emailaddress'];
$message = $_POST['message'];

$body = <<<EMAIL 

Hi $name, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site. 

From $name 

EMAIL;

$header = '$email';

if ($_POST){        
    mail($to, $subject, $body, $header);
    $feedback = 'Email Sent';
}

?>
  • 写回答

1条回答 默认 最新

  • dptsivmg82908 2016-05-29 14:15
    关注

    Firstly, variables do not get parsed inside single quotes, that's why you're seeing $email rather than the email itself inside the email body.

    So change $header = '$email'; to either $header = "$email"; or remove the quotes entirely.

    I.e.: $header = $email;

    Then the header is failing you. It expects to be a From: (email address) - The "From" in your mail will come back as your server's name rather than the (intended) email address from the person sending it.

    Consult the manual:

    Sidenote about your heredoc.

    Even though mail is going out, you still have a trailing space in your opening identifier:

    $body = <<<EMAIL

    and that may throw a Parse error: syntax error, unexpected '<<' (T_SL) error.


    Testing this came back as:

    email@example.com
    
    Hi Fred, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
    
    From Fred
    

    and the "From:" as default@cpanel.example.com rather than whoever@example_mailer.xxx.

    As noted in comments by dieend, you can try bracing the variables {$var}, however with or without them, produced the same results for me.

    If that still doesn't work, then it may be caused by the trailing space in your opening identifier; you need to remove it.

    Copy/paste exactly as shown:

    $body = <<< EMAIL
    
    Hi {$name}, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site. 
    
    From {$name} - {$email}
    
    EMAIL;
    
    $header = "From:" . $email; // Now you have a valid From
    
    if ($_POST){
        mail($to, $subject, $body, $header);
        echo $feedback = 'Email Sent';
    }
    

    Final notes:

    • If that still doesn't solve the question, then I for one am unable to reproduce.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号