带有变量>>>的PHP MAIL函数怎么样?

时间:2014-12-13 16:33:51

标签: php email variables smtp ssmtp

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

表单+输入和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      = 'fawfafafaf@gmail.com';
$subject = $_GET['subject_email'];
$message = $_GET['message_email'];
$headers = 'From: '$_GET['from_email'] . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
} ?>

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

<?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);
?> 

1 个答案:

答案 0 :(得分:1)

您可以使用phpmailer class 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   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password

    $mail->SetFrom('name@yourdomain.com', 'First Last');

    $mail->AddReplyTo("name@yourdomain.com","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 = "whoto@otherdomain.com";
    $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!";
    }