重定向时PHP邮件不能正常工作

问题描述:

我正在为我的网站创建一个联系表单,但我遇到了一些问题。它在没有使用位置的情况下工作正常,我收到一封电子邮件,但是当使用位置时,我在输入表单时没有收到电子邮件。重定向时PHP邮件不能正常工作

我该如何重定向并同时获取电子邮件?

HTML:

\t <div class="contact-form"> 
 
\t \t <div class="contact-form-container"> 
 
\t \t \t <form novalidate action="index.php" method="POST"> 
 
\t \t \t \t <label id="label-0">Position</label> 
 
\t \t \t \t <select name="position_form"> 
 
\t \t \t \t \t <option value="collaboration_partner">Samarbetspartner</option> 
 
\t \t \t \t \t <option value="youth_organization">Ungdomsorginisation</option> 
 
\t \t \t \t \t <option value="young_enterprise">Ung Företagsamhet</option> 
 
\t \t \t \t \t <option value="work_group">Arbetsgrupp</option> 
 
\t \t \t \t \t <option value="school">Skola</option> 
 
\t \t \t \t \t <option value="volunteer_trainee">Volontär/Praktikant</option> 
 
\t \t \t \t \t <option value="charity">Välgörenhet</option> 
 
\t \t \t \t \t <option value="sponsor">Sponsor</option> 
 
\t \t \t \t \t <option value="other">Annat</option> 
 
\t \t \t \t </select> 
 

 
\t \t \t \t <label id="label-1">Namn</label> 
 
\t \t \t \t <input name="name" placeholder="Namn" type="text"> 
 

 
\t \t \t \t <label id="label-2">Email Adress</label> 
 
\t \t \t \t <input name="email" type="text" placeholder="Email Adress"> 
 

 
\t \t \t \t <label id="label-3">Meddelande</label> 
 
\t \t \t \t <textarea name="message" placeholder="Meddelande"></textarea> 
 

 
\t \t \t \t <input id="submit" name="submit" type="submit" value="SKICKA"> 
 
\t \t \t </form> 
 
\t \t </div> 
 
\t </div>

PHP:

<?php 
 

 
$to = "[email protected]"; 
 
$subject = "Kontakta Oss - Senaste Meddelande"; 
 
$name = $_POST['name']; 
 
$email = $_POST["email"]; 
 
$position_form = $_POST["position_form"]; 
 
$message = $_POST["message"]; 
 

 

 

 
$body = <<<EMAIL 
 

 
Namn: 
 
$name 
 

 
Email: 
 
$email 
 

 
Position: 
 
$position_form 
 

 
Meddelande: 
 
$message 
 

 
EMAIL; 
 

 
$header = "from: [email protected]"; 
 
header('Location: http://example.com/redirecthere.php'); 
 
exit(); 
 

 
if($_POST){ 
 
\t mail($to, $subject, $body, $header); 
 
} 
 
?>

+1

你确实如何看待你的mail()调用会被执行,因为你有'exit()'事件? –

+0

也许如果你移动标题相关的代码到脚本的末尾.... – Hackerman

+0

谢谢你们,新来的,我很欣赏这个反馈。 –

结束尝试改变你的PHP文件是这样的:

<?php 

$to = "[email protected]"; 
$subject = "Kontakta Oss - Senaste Meddelande"; 
$name = $_POST['name']; 
$email = $_POST["email"]; 
$position_form = $_POST["position_form"]; 
$message = $_POST["message"]; 



$body = <<<EMAIL 

Namn: 
$name 

Email: 
$email 

Position: 
$position_form 

Meddelande: 
$message 

EMAIL; 

$header = "from: [email protected]"; 


if($_POST){ 
    mail($to, $subject, $body, $header); 
    header('Location: http://example.com/redirecthere.php'); 
    exit(); 
} 
?> 

我希望这可以帮助您。

顺便说一句,我建议你寻找像Mailgun,Sendgrid等交易电子邮件服务。

+0

非常感谢。真正意识到的是,第一个答案没有正常工作,但是确实如此。刚刚确认你的答案。 –

mail函数被调用(事实上,我t从来没有打过)在header('Location之后。即使您删除了header也不会发送邮件,因为在获取if之前调用了exit函数。

取出exit和移动header到文件

+0

谢谢,我做了你所说的,但它似乎没有工作。 编辑:没关系,它确实有效。非常感谢你。我会尽快接受你的回答。 –

+0

很高兴工作! –

+0

我非常感谢,我发现了Stackoverflow,它一直是一个惊人的帮助。事实上,我在不到六分钟的时间内得到了答案,这个答案本来需要几个小时才能理解。干杯。 –