使用PHP发送电子邮件,更改FROM,这不是我的服务器名称 [英] Using PHP to send e-mail, change the FROM so it's not my server name

查看:211
本文介绍了使用PHP发送电子邮件,更改FROM,这不是我的服务器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经为我的网站设置了一个联系表单,当人们注册时,它会向我发送电子邮件。以下是代码:

 <?php 
include(include / settings。 PHP的);
if(isset($ _ POST ['name'])&& isset($ _ POST ['contactEmail'])& isset($ _ POST ['message'])){
$ name = $ _POST ['name'];
$ from = $ _POST ['contactEmail'];
$ message = $ _POST ['message'];

$ subject =来自消息。 $名称;

if(mail($ to,$ subject,$ message,$ from)){
$ response = array('sent'=> 1);
echo json_encode($ response);
} else {
$ response = array('sent'=> 0);
echo json_encode($ response);
}
}
?>

现在,当我收到一封电子邮件时,它来自我的godaddy服务器名称。我不介意,如果它继续发送从这封电子邮件,但我需要将名称改为不同的东西。这是可能吗?

解决方案

您现在的头文件被解释为

@ example.com,来自John的消息,messsage,email@example.com

当它应该被解释为

you@example.com,来自John的消息,垃圾邮件,From:email@example.com



所以邮局主管说: 没有来自:这里,所以我只是假设你的意思是我的服务器。




  • 这是原因为什么from似乎来自GoDaddy服务器。



使用正确的标题: http://php.net/manual/en/function.mail.php

  $ headers =From:$ name。<。$ from。> \r\\\
;

然后将您现在的 mail()重写为: p>

mail($ to,$ subject,$ message,$ headers) / p>

如果您希望它不同,请使用不同的电子邮件地址。



Nota:邮件期望From成为一个电子邮件地址,而不是一个人的名字。



所以你可以这样做: p>

  $ from_other =another_email@example.com; 

然后执行:

  $ headers =From:。 $名称。 < 。 $ from_other。 > \r\\\
;

然后将您现在的 mail()重写:



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




  • 如果这是终极目标






还有额外的标题选项可供您使用:

  $ from = $ from。 '<'。 $从。 >; 
$ headers =MIME-Version:1.0。 \r\\\
;
$ headers。=Content-type:text / html; charset = utf-8。 \r\\\
;
$ headers。='From:'。 $从。 \r\\\
;
$ headers。='Reply-To:'。 $从。 \r\\\
;
$ headers。='返回路径:'。 $从。 \r\\\
;
$ headers。='X-Mailer:PHP /'。 phpversion()。 \r\\\
;
$ headers。='X-Originating-IP:'。 $ _SERVER ['SERVER_ADDR']。 \r\\\
;




  • 使用回复:如果你想使用不同的回复。那些变量可以改变为你想要的变量。


So, I've set up a contact form for my website, and it's sending me e-mails when people sign-up. Here's the code:

<?php
include("include/settings.php");
if(isset($_POST['name']) && isset($_POST['contactEmail']) && isset($_POST['message'])){
    $name = $_POST['name'];
    $from = $_POST['contactEmail'];
    $message = $_POST['message'];

    $subject = "Message from " . $name;

    if (mail ($to, $subject, $message, $from)) { 
        $response = array('sent' => 1);
        echo json_encode($response);
    } else { 
        $response = array('sent' => 0);
        echo json_encode($response);
    } 
}
?>

Right now when I get an e-mail, it's from my godaddy servername. I don't mind if it continues to send from this email, but I need to have the from name changed to something different. Is this possible?

解决方案

Your present headers are being interpreted as
you@example.com, Message from John, the messsage, email@example.com
when it should be interpreted as
you@example.com, Message from John, the messsage, From: email@example.com

So the postmaster says: "There's no From: here, so I'll just assume you meant "my server".

  • That is the reason why the "from" appears to be coming from the GoDaddy server.

Use proper headers: http://php.net/manual/en/function.mail.php

$headers = "From: ". $name . " <" . $from . ">\r\n";

then rewrite your present mail() to read as:

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

If you want it to be something different, then use whatever different Email address.

Nota: Mail expects a "From" to be an email address and not a person's name.

So you could do:

$from_other = "another_email@example.com";

then do:

$headers = "From: ". $name . " <" . $from_other . ">\r\n";

then rewrite your present mail() to:

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

  • If that's what the ultimate goal is.

There are additional header options that are also available for you to use:

$from = $from . ' <' . $from . '>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'] . "\r\n";

  • Using Reply-To: if you wish to use a different "reply to". Those variables can be changed to whatever you wish them to be.

这篇关于使用PHP发送电子邮件,更改FROM,这不是我的服务器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆