2012-02-28 111 views
12

設置電子郵件發件人的名字所以我想from場當電子郵件被打開,是這樣的我如何通過PHP

"Jack Sparrow Via somesite"而不是一個明確的電子郵件地址。

我想知道如何在PHP的mail()函數中設置它?

回答

9

你必須設置你的頭:

$to = "[email protected]"; 
$header = "FROM: Jack Sparrow <[email protected]>\r\n"; 
$message = "Your message here."; 
$subject = "Your subject"; 

mail($to,$subject,$message,$header) or die(); 
28

您可以使用基本的頭做到這一點。

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: Jack Sparrow <[email protected]>' . PHP_EOL . 
    'Reply-To: Jack Sparrow <[email protected]>' . PHP_EOL . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 
?> 
+3

**注:**當你想要一個'\ r \ N'它能夠更好地使用'PHP_EOL'使它兼容所有系統 – 2012-02-28 19:27:34

+1

這是否仍然適用於此?據我所知,PHP_EOL給出了服務器使用的行結束符,但這應該專用於電子郵件標題的標準,而不是服務器。 – 2016-04-11 18:11:29

2

只是用這樣

$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n"; 
在報頭部分

2

如果你從GitHub使用PHP梅勒,然後你要做到這一點:

$mail->SetFrom("[email protected]", "MIBC");