简体   繁体   中英

PHP mail variable in header From:

I have the following php script:

$odesilatel = $_SESSION['odesilatel']; //this works, $odesilatel displays the right content
$komu = "my@emailadress.cz, mysecond@emailadress.cz";
$predmet = "Objednávka z newsletteru";

$hlavicka = 'From: '.$odesilatel. "\n";
$hlavicka .= 'MIME-Version: 1.0' . "\n"; 
$hlavicka .= 'Content-type: text/html; charset=UTF-8' . "\n"; 
$hlavicka .= 'Content-Transfer-Encoding: 8bit' . "\n";

$zprava = "I have some HTML code here. It's not important what it is...";

if(@mail($komu, $predmet, $zprava, $hlavicka)){
   echo 'E-mail byl úspěšně odeslán. Děkujeme za objednávku.';}
else{
  echo 'E-mail se bohužel nepodařilo odeslat.';}

If I delete the variable from the part of header where From: is and write here some email adress the mail is delivered. But if I use the variable, the mail is sent (echo is displayed), but it is not delivered. How to write correctly the header with From: and the variable?

Thank you very much for your help.

Bc. Michal Vlasák

Try setting the MIME-Version and Content-type first.

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //or another encoding
$headers .= 'From: ' . $variable .  "\r\n";

I tested your code and it worked with what I have for code below, adding session_start(); as well as my own Email address.

What I think may be happening is that, and since the From Email address is coming from a session name, and session_start(); may not be included in your code, and it was not shown in yours, the Email From is empty and has not been set.

(I would need to see your full code for both your HTML form and PHP.)

Successful (tested) code:

<?php
session_unset(); // unset any previously set session and start a new one after
session_start(); // required when using sessions
// session variable set to Email (From) address
// Which could be coming from a POST variable
// $_SESSION['odesilatel'] = $_POST['from_email']; // example
$_SESSION['odesilatel'] = "email@example.com";

// check if session is empty
if(empty($_SESSION['odesilatel'])){ die("Session name is not set, stopping execution of script."); }

$odesilatel = $_SESSION['odesilatel']; //this works, $odesilatel displays the right content
$komu = "my@emailadress.cz, mysecond@emailadress.cz";
$predmet = "Objednávka z newsletteru";
$hlavicka = 'From: '.$odesilatel. "\n";
$hlavicka .= 'MIME-Version: 1.0' . "\n"; 
$hlavicka .= 'Content-type: text/html; charset=UTF-8' . "\n"; 
$hlavicka .= 'Content-Transfer-Encoding: 8bit' . "\n";
$zprava = "I have some HTML code here. It's not important what it is...";

if(mail($komu, $predmet, $zprava, $hlavicka)){
   echo 'E-mail byl úspešne odeslán. Dekujeme za objednávku.';

echo $_SESSION['odesilatel']; // echo'ed my Email address
}
else{ echo 'E-mail se bohužel nepodarilo odeslat.';}

What I suggest you do is to set a conditional statement to check whether or not the session name is set or not, or better yet to check if it is empty.

Ie:

if(!isset($_SESSION['odesilatel'])){ die("Session name is not set, stopping execution of script."); }

but if session is empty, for example $_SESSION['odesilatel'] = ""; mail will still be sent, but will end up in SPAM since the From is not being recognized.

Therefore using if(empty would be a better option.

if(empty($_SESSION['odesilatel'])){ die("Session name is not set, stopping execution of script."); }

I also suggest you do a var_dump();

Ie:

var_dump($_SESSION['odesilatel']);

or:

var_dump($_SESSION);

to see what comes up, and if it is empty, then that would signal a problem as well as show you if the From is in fact set or empty.

And finally email.php that send the data by the e-mail. There are used your suggestions.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
  <HEAD>
  <META http-equiv="content-type" content="text/html; charset=utf-8">
  <META name="generator" content="PSPad editor, www.pspad.com">
  <TITLE>Potvrzení údajů</TITLE>
  <STYLE type="text/css">
    .tabulkaobjednavka{border: 1px solid #339935;}
    .tabulkanadpis{font-family: sans-serif; font-size: 14px; color: #ffffff; background-color: #339935; padding-left: 5px;}
    .bunkyobjednavky{font-family: serif; font-size: 14px; color: #339935; padding-left: 5px;}
  </STYLE>
  </HEAD>
  <BODY>
  <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    session_start();

    if(isset($_POST['potvrdit'])){
      $vypis = $_SESSION['vypis'];
      $odesilatel = $_SESSION['odesilatel'];
     if(empty($_SESSION['odesilatel'])){ die("Session name is not set, stopping execution of script.");}

      $komu = "michal.vlasak@1sdzp.cz";
      $predmet = "Objednávka z newsletteru";
      $headers = 'MIME-Version: 1.0' . "\n"; 
      $headers .= 'Content-type: text/html; charset=utf-8' . "\n"; 
      $headers .= 'From: ' . $odesilatel;
      $zprava = 
      "
      <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
      <HTML>
        <HEAD>
        <META http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
        <STYLE type=\"text/css\">
          td.tabulkaobjednavka{border: 1px solid #339935;}
          td.tabulkanadpis{font-family: sans-serif; font-size: 14px; color: #ffffff; background-color: #339935; padding-left: 5px;}
          td.bunkyobjednavky{font-family: serif; font-size: 14px; color: #339935; padding-left: 5px;}
        </STYLE>
        <TITLE>E-mail</TITLE>
        </HEAD>
        <BODY>".$vypis."</BODY>
      </HTML>";

      if(mail($komu, $predmet, $zprava, $headers)){
            echo '<P class="bunkyobjednavky">E-mail byl úspěšně odeslán. Děkujeme za objednávku.</P>';}
      else{
            echo '<P class="bunkyobjednavky">E-mail se bohužel nepodařilo odeslat.</P>';}}
  ?>
</BODY>
</HTML>

I don't know what to do. I think about it over the month.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM