2014-12-13 32 views
-2

我创建了一个contact.php。 我的计划是在用户之前输入的电子邮件内容中包含。 (变量) 在这种情况下,包括发件人电子邮件,主题和消息。 我不想用PEAR。我想使用我的Raspberry Pi上安装的Debian上的SSMTP。带变量的PHP MAIL函数>>>如何?

表单+输入和textarea的

代码:

<form method="POST" action="contact.php?page=log"> 
<input type="text" name="from_email" placeholder="Your E-Mail"/> 
<input type="text" name="subject_email" placeholder="Subject"/> 
<textarea rows="5" cols="50" name="message_email" style="width: 100%" placeholder="Message"></textarea> 
<input type="submit" name="submite_email" value="Send E-Mail" /> 
</form> 

代码PHP电子邮件:

<?php 
if(isset($_POST['from_email']) and isset($_POST['subject_email']) and isset($_POST['message_email'])){ 
$to  = '[email protected]'; 
$subject = $_GET['subject_email']; 
$message = $_GET['message_email']; 
$headers = 'From: '$_GET['from_email'] . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
mail($to, $subject, $message, $headers); 
} ?> 

ssmtp中和php.ini中设置。 如果我为此使用默认表单,则会向我发送电子邮件。

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

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

这是这个网站不是如何工作,你需要点击刻度是回答你的问题的答案旁边,而不是创建一个新的“答案”。 – cybermonkey 2014-12-14 12:56:57

回答

1

您CA使用的PHPMailer类https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

require_once('../class.phpmailer.php'); 

    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

    $mail    = new PHPMailer(); 

    $body    = file_get_contents('contents.html'); 
    $body    = eregi_replace("[\]",'',$body); 

    $mail->IsSMTP(); // telling the class to use SMTP 
    $mail->Host  = "mail.yourdomain.com"; // SMTP server 
    $mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
               // 1 = errors and messages 
               // 2 = messages only 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->Host  = "mail.yourdomain.com"; // sets the SMTP server 
    $mail->Port  = 26;     // set the SMTP port for the GMAIL server 
    $mail->Username = "[email protected]"; // SMTP account username 
    $mail->Password = "yourpassword";  // SMTP account password 

    $mail->SetFrom('[email protected]', 'First Last'); 

    $mail->AddReplyTo("[email protected]","First Last"); 

    $mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication"; 

    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

    $mail->MsgHTML($body); 

    $address = "[email protected]"; 
    $mail->AddAddress($address, "John Doe"); 

    $mail->AddAttachment("images/phpmailer.gif");  // attachment 
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

    if(!$mail->Send()) { 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
     echo "Message sent!"; 
    } 
+0

谢谢,但我想使用已经安装在我的Raspberry Pi上的我的ssmtp。我只需要知道如何将$ _POST中的东西包含到它使用的地方。我会稍后再尝试。再次,谢谢:) – Bird 2014-12-13 17:01:23

+0

$ subject = $ _GET ['subject_email'];改为 $ subject = $ _POST ['subject_email']; – 2014-12-13 17:14:08

+0

你正在使用$ _GET而不是$ _POST $ _GET在POST请求中为空 – 2014-12-13 17:15:58

相关问题