dppi5167 2016-05-29 05: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 06: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.

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部