发送邮件给DB

时间:2013-12-02 14:19:04

标签: php html mysql forms

我想知道是否可以向数据库发送电子邮件?例如,如果您有比赛,当人们猜测您的问题或其中的一部分时,他们在提交表单中的输入应该保存在文件中的数据库中,易于打开,以备日后使用。这可能吗?

1 个答案:

答案 0 :(得分:0)

使用php mail class()

中的示例

你可以让一个类获取所有邮件数据:

<?php
class mailProcessor {

    public function _construct(){
        $to = "";
        $subject = "";
        $message = "";
    }

    public function processEmail($to, $subject, $message){
        //so validation here for the strings being inputed
        //
        if($to){
            $this->to = $to;

        }

        //and so on

    }

    //if evertying is okay require the the mail class
    require 'PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'jswan';                            // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

    $mail->From = 'from@example.com';
    $mail->FromName = 'Mailer';
    $mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {

        if($this->updateDatabase())
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }

    public function updateDatabase(){
        //get all local variables from the cosntructor
        //
        //update the databse with the new data
        //
        return true

        //else should return false..
    }
}


?>

所以需要进行更改,但这个想法将足以让这项工作完成:)