【问题标题】:PHP Mail Using SMTP taking info from HTML formPHP Mail 使用 SMTP 从 HTML 表单中获取信息
【发布时间】:2014-09-04 02:42:08
【问题描述】:

我最近移动了主机,现在通过联系表单我必须使用 SMTP。我已经能够在一些帮助下完成工作,但无法从表单中检索到我需要的信息。我试图提取的信息是姓名电子邮件电话和消息。这是我一直在使用的

<?php

require "email.php";

$mail = new EMail;
$mail->Username = 'username.co.uk';
$mail->Password = 'mypassword';

$mail->SetFrom("no-reply@mywebsite.co.uk","no-reply@mywebsite.co.uk");  // Name is      optional
$mail->AddTo("no-reply@mywebsite.co.uk","no-reply@mywebsite.co.uk"); // Name is optional
$mail->Subject = "Hello World Again!";
$mail->Message = "Hello World!";

//Optional
$mail->ContentType = "text/html";          // Defaults to "text/plain; charset=iso-8859-1"
$mail->Headers['X-SomeHeader'] = 'abcde';  // Set some extra headers if required
$mail->ConnectTimeout = 30;  // Socket connect timeout (sec)
$mail->ResponseTimeout = 8;  // CMD response timeout (sec)
$success = $mail->Send();

?>

当我对此进行测试时,会发送一封电子邮件,但当然只会发送“Hello World”文本,因为我不确定在哪里放置必要的代码来获取我需要的信息。

在移动主机并且不需要 SMTP 之前,我正在使用此脚本

<?php

//-----------------------------------------------------
//-----------------------------------------------------
$address= "myemail.co.uk";
//-----------------------------------------------------
//-----------------------------------------------------

$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$phone = $_REQUEST["phone"];
$message_content =        $_REQUEST["message"];
$mime_boundary = md5(time());

$headers = "From: $name   <$email>\n";
$headers .= "Reply-To: $subject <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type:   multipart/alternative;   boundary=\"$mime_boundary\"\n";

$message = "--  $mime_boundary\n\n";
$message .= "New email from the  Commons website: \n\n\n";
$message .= "Name: $name \n\n";
$message .= "Email: $email \n\n";
$message .= "Phone: $phone \n\n";
$message .= "Message:     $message_content \n\n";

$message .= "--$mime_boundary--\n\n";

$mail_sent = mail($address,   $subject, $message, $headers);
echo $mail_sent ? "Success, mail   sent!" : "Mail failed";
?>

提前致谢

亲切的问候

提姆

【问题讨论】:

  • 为什么不用php邮件功能??
  • 我不确定我是否关注

标签: php email smtp


【解决方案1】:

你试过这样吗???

 $message_str = str_replace("[first_name]", $fname, $message_str);
                            $message_str = str_replace("[last_name]", $lname, $message_str);
                            $message_str = str_replace("[site_name]", "RMS", $message_str);

// Always set content-type when sending HTML email
                            $headers = "MIME-Version: 1.0" . "\r\n";
                            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
                            $headers .= 'From: <norply@rms.com>' . "\r\n";


                            $subject = "activate account!";
                            if (mail("asd@sad.com", $subject, $message_str, $headers)) {
                                echo"Email successfully sent!";
                            } else {
                                echo"Email delivery failed…";
                            }

【讨论】:

  • 嗨,Arham,我可以在上面添加我的 SMTP 登录信息吗?
  • 嗨,Arham 我使用下面的代码可以提供我需要的信息,但它不会发送。我错过了什么吗?
  • 您是否通过 asd@sad.com 传递了收件人邮件 ID?
【解决方案2】:

只需使用 Php Mail 功能

考虑下面的代码

$subject = 'Welcome To demo.com';

    $mydata['name'] = 'demo';
    $mydata['user_name'] = 'test1';
    $mydata['email'] = test@gmail.com;
    $mydata['password'] = $password;
    $mydata = (object)$mydata;
                $message = "<div style='height: 500px; background: none repeat scroll 0% 0% rgb(241, 241, 241); border: 1px solid rgb(6, 106, 117); width: 450px;'>
                <div style='width: 100%; background: none repeat scroll 0% 0% rgb(6, 106, 117); height: 80px;'>
                <div style='width:150px; height:100px;margin:8px; float:left'>
                </div>
                </div>
                  <div style='width:100%;height:360px; margin: auto; float:left; text-align: center;'>
                  <h2>Hello $mydata->name </h2>

                  <h3>Here is you login details for Demo</h3>
                  <br>
                  <h3>Username: $mydata->user_name</h3>

                  <h3>Password: $mydata->password</h3>
                  </div>
                  <div style='height:30px;padding-top:10px;float:left; width:100%;text-align:center;background:#1d5770;'>
                    <span style='color:#fff;font-weight:bold'>demo Team</span>
                  </div>
                </div>";
    //$message = "<br><br>Username:$mydata->user_name <br><br><br>Password:$mydata->password <br>";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "To: $mydata->name <$mydata->email>" . "\r\n";
    $headers .= "From: demo.com <contact@demo.com>" . "\r\n";

    mail($mydata->email, $subject, $message, $headers);

更多参考请点击这些链接

http://www.w3schools.com/php/php_ref_mail.asp
http://php.net/manual/en/function.mail.php

【讨论】:

    【解决方案3】:

    我使用了这个,但是我收到的消息是电子邮件失败

    <?php
    
    require "email.php";
    
    $mail = new EMail;
    $mail->Username = 'myemail.co.uk';
    $mail->Password = 'password';
    
    $name = $_REQUEST["name"];
    $email = $_REQUEST["email"];
    $phone = $_REQUEST["phone"];
    $message_content =        $_REQUEST["message"];
    $mime_boundary = md5(time());
    
    $headers = "From: $name   <$email>\n";
    $headers .= "Reply-To: $subject <$email>\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type:   multipart/alternative;   boundary=\"$mime_boundary\"\n";
    
    $message = "--  $mime_boundary\n\n";
    $message .= "New email from the  Commons website: \n\n\n";
    $message .= "Name: $name \n\n";
    $message .= "Email: $email \n\n";
    $message .= "Phone: $phone \n\n";
    $message .= "Message:     $message_content \n\n";
    
    $message .= "--$mime_boundary--\n\n";
    
    echo $mail_sent ? "Success, mail   sent!" : "Mail failed";
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      相关资源
      最近更新 更多