8792 sujets

Développement web côté serveur, CMS

Bonjour,

il se trouve que je suis débutant en PHP, et j'ai mis en place un formulaire php qui fonctionne, cependant lorsque je repond au mail envoyé par l'internaute via mon formulaire, j'aimerai pouvoir directement repondre au mail indiqué par l'internaute dans le formulaire.

Actuellement, je ne peux repondre qu'à l'adresse de mon serveur, comme cet exemple: monsite@60gp...net

Merci pour votre aide.

Voici le code de ma page php:

<?php



################################################################################

$g_check_sender = true;
$g_valid_sites = array("http://www.monsite.com");

################################################################################



// program control flow
onload();

function onload()
{
global $g_check_sender, $g_valid_sites;

// check required fields
if (!isset($_POST['recipient']) || empty($_POST['recipient']))
display_error("no_recipient");
elseif (!isset($_POST['redirect_page']) || empty($_POST['redirect_page']))
display_error("no_redirect_page");

// check recipient email is valid
elseif (!is_valid_email($_POST['recipient']))
display_error("invalid_recipient");


if ($g_check_sender)
{
// get the referer domain and check it's valid
$referer = get_domain($_SERVER['HTTP_REFERER']);
$found = false;
foreach ($g_valid_sites as $site)
{
if ($referer == get_domain($site))
$found = true;
}

// if the domain wasn't listed, display error message
if (!$found)
display_error("domain_not_found");
}

// send the email
send_email();
}



/*----------------------------------------------------------------------------*\
Function: send_email
Purpose: extracts the contents of the POST form fields and tries to send
an email.
Assumptions: "redirect_page" and "recipient" are both contained in $_POST
\*----------------------------------------------------------------------------*/
function send_email()
{
// prepare search-replace values
$patterns = array_keys($_POST);
$replacements = array_values($_POST);
array_walk($patterns, "prepare_regexp");
array_walk($replacements, "encode_chars");

$to = $_POST['recipient'];
unset($_POST['recipient']);

$redirect_page = $_POST['redirect_page'];
unset($_POST['redirect_page']);


// determine email subject
$email_subject = '';
if (isset($_POST['email_subject']))
{
$email_subject = $_POST['email_subject'];
$email_subject = preg_replace($patterns, $replacements, $email_subject);
$email_subject = remove_empty_placeholders($email_subject);
unset($_POST['email_subject']);
}


// build our email header;
$header = "";
$recipient_header = "";
if (isset($_POST['email_from']))
{
$header .= "From: " . $_POST['email_from'] . "\r\n";
$recipient_header .= "From: " . $_POST['email_from'] . "\r\n";
unset($_POST['email_from']);
}

if (isset($_POST['cc']))
{
$header .= "Cc: " . $_POST['cc'];
unset($_POST['cc']);

while (list($key, $value) = each($_POST))
{
if (preg_match("/^cc(\d+)$/i", $key))
{
// if the email is invalid, just ignore it
if (is_valid_email($value))
$header .= ", " . $value;
unset($_POST[$key]);
}
}
$header .= "\r\n";
}


reset($_POST);
if (isset($_POST['bcc']))
{
$header .= "Bcc: " . $_POST['bcc'];
unset($_POST['bcc']);

while (list($key, $value) = each($_POST))
{
if (preg_match("/^bcc(\d+)$/i", $key))
{
// if the email is invalid, just ignore it
if (is_valid_email($value))
$header .= ", " . $value;
unset($_POST[$key]);
}
}
$header .= "\r\n";
}

if (isset($_POST['reply_to']))
{
$header .= "Reply-To: " . $_POST['reply_to'] . "\r\n";
unset($_POST['reply_to']);
}

// assemble email message
$message = '';

if (isset($_POST['email_template']) && is_file($_POST['email_template']))
{
$template = file_get_contents($_POST['email_template']);
$message = preg_replace($patterns, $replacements, $template);

// remove any redundant placeholders
$message = preg_replace("/%DOLLAR%/", "$", $message);
$message = preg_replace("/%[^\s]*%/", "", $message);
}

// otherwise, no template is defined - just generate an email containing all the content
else
{
reset($_POST);
while (list($key, $value) = each($_POST))
{
$key = preg_replace("/_/", " ", $key);
$message .= "$key: $value\n";
}
}

// send email
if (!mail($to, $email_subject, $message, $header))
display_error("cannot_send_email");


// if a recipient template has been defined, send that email too.
if (isset($_POST['receipt_template']) && is_file($_POST['receipt_template']))
{
$receipt_to = $_POST[$_POST["receipt_email_field"]];

// assume that the recipient email field isn't required
if (!empty($receipt_to))
{
$receipt_email_subject = $_POST['receipt_email_subject'];
$receipt_email_subject = preg_replace($patterns, $replacements, $receipt_email_subject);
$receipt_email_subject = remove_empty_placeholders($receipt_email_subject);
unset($_POST['receipt_email_subject']);

$template = file_get_contents($_POST['receipt_template']);
$message = preg_replace($patterns, $replacements, $template);

// remove any redundant placeholders
$message = preg_replace("/%DOLLAR%/", "$", $message);
$message = preg_replace("/%[^\s]*%/", "", $message);

if (!mail($receipt_to, $receipt_email_subject, $message, $recipient_header))
display_error("cannot_send_recipient_email");
}
}


// redirect user to appropriate page
header("Location: $redirect_page");
exit;
}



/*----------------------------------------------------------------------------*\
Function: get_domain
Purpose: helper function to strip everything but the domain (and any
subdomains for a given URL.
e.g. http://www.whatever.com/text.html -> whatever.com
www.sub.mysite.com/text.html -> sub.mysite.com
\*----------------------------------------------------------------------------*/
function get_domain($string)
{
$clean = preg_replace("/^https?:\/\/(www\.)?/", "", $string);
$clean = preg_replace("/\/.*$/", "", $clean);

return $clean;
}


/*----------------------------------------------------------------------------*\
Function: is_valid_email
Purpose: checks to make sure an email address is valid.
\*----------------------------------------------------------------------------*/
function is_valid_email($email)
{
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}


/*----------------------------------------------------------------------------*\
Function: prepare_regexp
Purpose: helper function to convert a string for preparation as being searched
for in a regular expression.
\*----------------------------------------------------------------------------*/
function prepare_regexp(&$item, $key)
{
$item = "/%$item%/";
}

/*----------------------------------------------------------------------------*\
Function: encode_chars
Purpose: helper function to encode $ characters so they're not lost
\*----------------------------------------------------------------------------*/
function encode_chars(&$item, $key)
{
$item = preg_replace("/\\$/", "%DOLLAR%", $item);
}


/*----------------------------------------------------------------------------*\
Function: remove_empty_placeholders
Purpose: helper function to remove all the placeholder that weren't found
and replaced in a template - or a string.
Bug!: This will cause problems...
\*----------------------------------------------------------------------------*/
function remove_empty_placeholders($string)
{
return $string;
}


/*----------------------------------------------------------------------------*\
Function: display_error
Purpose: displays an error based on the particular error message flag
specified by the incoming parameter. After outputting the message,
exits the program.
\*----------------------------------------------------------------------------*/
function display_error($message_flag)
{

$message_html = '';
switch($message_flag)
{
case "no_recipient":
$message_html = "<p>No recipient email address has been specified by the "
. "form page. This specifies who will receive the information contained "
. "in the form.</p>"
. "<p>To solve this problem, take a look at your form and make sure "
. "it contains the following hidden field:</p>"
. "<p class='html'>&lt;input type='hidden' name='recipient' "
. "value='<span class=\"replace_text\">enter_email@address.here</span>' /&gt;</p>"
. "<p>[And be sure to include the correct recipient email address!]</p>";
break;

case "invalid_recipient":
$message_html = "<p>The recipient email address is invalid: "
. "<b>" . $_POST['recipient'] . "</b>. Please "
. "check to make sure the following tag in your form page contains a "
. "valid email address:</p>"
. "<p class='html'>&lt;input type='hidden' name='recipient' "
. "value='<span class=\"replace_text\">enter_email@address.here</span>' /&gt;</p>";
break;

case "no_redirect_page":
$message_html = "<p>No redirect page has been specified in the form page. This "
. "lets you choose where the user will be redirected to after the "
. "email is sent by this script.</p>"
. "<p>To solve this problem, add the following hidden field to your "
. "form page:</p>"
. "<p class='html'>&lt;input type='hidden' name='redirect_page' "
. "value='<span class=\"replace_text\">FULL_URL_HERE'</span> /&gt;</p>";
break;

case "domain_not_found":
$message_html = "<p>This referral domain has not been specified in the script. "
. "Please see the <b>readme.txt</b> file for instructions on how to "
. "properly configure the script to be allowed to receive form data from "
. "this URL.</p>";
break;

case "cannot_send_email":
$message_html = "<p>The email could not be sent. This usually means that either PHP "
. "has not been configured to sent emails on this server or the service is "
. "temporarily down</p>";
break;

case "cannot_send_recipient_email":
$message_html = "<p>The recipient email could not be sent.</p>";
break;

default:
$message = "Unknown error flag.";
}


echo <<<OUTPUT

<html>
<head>
<title>An error has occurred</title>

<style type='text/css'>
p, table, td { font-family: arial; font-size: 8pt; }
.html { margin: 15px; font-family: courier; }
#error_title { font-weight: bold; color: white; }
.replace_text { color: green; }
</style>

</head>
<body>

<table width='400' height='100' align='center' valign='center'
style='border: 1px solid #999999; background-color: #efefef;'>
<tr>
<td height='10' bgcolor='#336699' id='error_title'>&nbsp;Error:</td>
</tr>
<tr>
<td>$message_html</td>
</tr>
</table>

</body>
</html>

OUTPUT;

exit();

}

?>
Salut,

apres avoir parcouru rapidement ce code je vois rien de spécifique qui te limiterait a tes adresses locales.

bon maintenant je ne suis pas un spécialiste en contenu de header de mail.

ca pourrait être un probleme de configuration smtp.

tu as bien défini SMTP et sendmail_from? et tu n'as pas un firewall ou je ne sais quoi...

pascal

P.S. : je te dirais bien aussi d'editer ton code en utilisant le bouton code.... même pas sur que ca rende la chose plus facile a lire Smiley lol les modo te le demanderons peut-etre quand même.
Modifié par CPascal (03 Jun 2007 - 22:04)
Salut Smiley cligne ,

A priori pour faire ce que tu veux il suffit d'alimenter ta variable $_POST['reply_to'] avec le mail de ton visiteur.

Si cette réponse ne règle pas ton problème il va falloir montrer ton formulaire Smiley langue
CPascal a écrit :
P.S. : je te dirais bien aussi d'editer ton code en utilisant le bouton code.... même pas sur que ca rende la chose plus facile a lire Smiley lol les modo te le demanderons peut-etre quand même.

+1 Smiley ravi
merci pour vos réponses, cependant je ne comprend pas comment utiliser cette variable $_POST['reply_to']
pourriez vous m'expliquer comment intégrér cette variable dans mon code php?

merci.
je viens à nouveau de tester mon formulaire et désormais je peux répondre à l'adresse indiqué par l'internaute. Pourtant je n'ai rien modifié dans le code. Donc tant mieux.

Cependant je voudrais que le mail de l'internaute apparaisse dans le mail comme l'adresse mail source qui a permis d'envoyer le messsage. Car aujourd'hui, tous les mails que je recois, indiquent à la place l'adresse mail source, mon identifiant de mon hébergement (par exemple mon identifiant ovh).

Merci pour vos réponses.
$header = "";
$recipient_header = "";
if (isset($_POST['email_from'])) 
{
$header .= "From: " . $_POST['email_from'] . "\r\n";
$recipient_header .= "From: " . $_POST['email_from'] . "\r\n";
unset($_POST['email_from']);
}


tu lui passes quoi a ton script comme valeur de $_POST['email_from']?

je dis peut-être une erreur mais l'adresse de l'expediteur doit correspondre a cette variable.
Salut,

A priori il suffit de supprimer
if (isset($_POST['reply_to']))
{
$header .= "Reply-To: " . $_POST['reply_to'] . "\r\n";
unset($_POST['reply_to']);
}
et de rajouter
if (isset($_POST['email_from']))
{
$header .= "From: " . $_POST['email_from'] . "\r\n";
[b]$header .= "Reply-To: " . $_POST['email_from'] . "\r\n";[/b]
$recipient_header .= "From: " . $_POST['email_from'] . "\r\n";
[b]$recipient_header .= "Reply-To: " . $_POST['email_from'] . "\r\n";[/b]
unset($_POST['email_from']);
}


*Edit: on s'est croisés Smiley cligne
Modifié par Heyoan (06 Jun 2007 - 22:05)
merci pour ces réponses.
Cependant rien n'a changé lorsque j'ai mis ce code
if (isset($_POST['email_from']))

{
$header .= "From: " . $_POST['email_from'] . "\r\n";
$header .= "Reply-To: " . $_POST['email_from'] . "\r\n";
$recipient_header .= "From: " . $_POST['email_from'] . "\r\n";
$recipient_header .= "Reply-To: " . $_POST['email_from'] . "\r\n";
unset($_POST['email_from']);
}

Y aurait t il un autre moyen d'y arriver?

D'ailleurs je me suis rendu compte que désormais, lorsque je répond à l'adresse source du formulaire, le probleme de départ se représente: ce n'est plus l'adresse mail indiqué par l'internaute, mais mon adresse mail du type monsite@ovh-60gp... Faut il attendre un temps précis après mise en ligne du formulaire pour que le mail source de l'internaute sois pris en compte?

Merci encore pour vos réponses
Le plus simple est d'afficher la source de ton mail et de rechercher ce que tu as en face de From: et Reply-to:

Et oui, parfois il faut un peu de temps (quelques petites minutes Smiley cligne ) avant que ta nouvelle page soit prise en compte...
Modifié par Heyoan (06 Jun 2007 - 22:35)
Merci à tous pour vos réponses.

Il est vrai que j'aurai souhaité avoir un formulaire entièrement automatique qui m'éviterait de regarder les champs from et reply to.

Peut être pourriez vous me transmettre un formulaire php que je pourrais utiliser sans probleme?

Merci