PHP:用我的网站域名发送电子邮件

时间:2013-06-24 01:31:22

标签: php

当我使用mail()功能并从我的网站向我自己发送电子邮件时。我收到了来自myusername@pickens.dreamhost.com的电子邮件,我想将发件人更改为noreply@mydomain.com

但有办法吗?

修改

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;

mail($to,$subject,$message,$headers);
echo "Mail Sent.";

这只会在邮件标题中显示from: noreply@mydomain.com。而已。但真正的发件人仍然是myusername@pickens.dreamhost.com

编辑2

我找到了答案......你可以在这里找到它: How to change envelope from address using PHP mail?

感谢AlvinArulselvan

3 个答案:

答案 0 :(得分:1)

您需要为出站邮件添加额外的标头

$to = "target@example.com";
$subject = "Subect";
$body = "This is my message";
$headers = "From:noreply@mydomain.com";

$result = mail($to, $subject, $body, $headers);

答案 1 :(得分:1)

您可以在发送电子邮件时使用标题更改发件人。这些基本的东西在mail()

的PHP手册页上有很好的说明

这是从该手册页

中取得的一个例子
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" . 
            // ^ This is where you update your header to show the sender
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

答案 2 :(得分:0)

您的mail()功能可能是这样的:

<?php
   $to = "someone@example.com";
   $subject = "Test mail";
   $message = "Hello! This is a simple email message.";
   $from = "someonelse@example.com";
   $headers = "From:" . $from;

   mail($to,$subject,$message,$headers);
   echo "Mail Sent.";
?>

尝试查找$from并更改为您首选的电子邮件