设置From:在Email中通过PHP邮件功能

时间:2012-02-04 07:19:36

标签: php email

我正在使用邮件功能

$headers = "MIME-Version: 1.0" . "\r\n";  
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";  
$headers .= 'From: <>' . "\r\n";  
$headers .= 'Cc: ' . "\r\n";  
$mail_sent = @mail( $to, $subject, $message, $headers ); 

我想将“From:”的电子邮件值作为变量传递。可以这样做吗?我怎样才能将手机号码等其他值传递给邮件?

2 个答案:

答案 0 :(得分:4)

将电子邮件地址($ fromAddress)作为标题传递

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$fromAddress.'>' . "\r\n".
             'Reply-To:'.$fromAddress. "\r\n" .
$headers .= 'Cc: ' . "\r\n";
$mail_sent = @mail( $to, $subject, $message, $headers );

答案 1 :(得分:2)

来自example2 in the docs ..

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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