PHP 联系表最佳实践

发布于 2024-10-08 20:39:07 字数 249 浏览 6 评论 0原文

我正在网站上设置 PHP 联系表单。我使用 Swift Mailer 库发送邮件,而我的域电子邮件是通过 Google Apps 发送的。是否可以使用 Google Apps 作为公司电子邮件并使用我的 VPS 上的 sendmail/SMTP 从联系页面发送电子邮件?我遇到的问题是我无法动态生成发件人地址,谷歌的服务器强制该地址成为电子邮件正在通过的电子邮件地址。提前致谢。

I'm setting up a PHP contact form on a site. I'm using the Swift Mailer library to send the mail, and my domain email is through Google Apps. Is it possible to use Google Apps for company email and use sendmail/SMTP on my VPS to send email from the contact page? The problem I'm having is that I can't dynamically generate the from address, Google's servers force that to be the email address that the email is going through. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-10-15 20:39:07

我使用 PHPMailer 具有此功能...

function email($to, $subject, $body){
    require_once("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465;
    $mail->Username = "[email protected]";
    $mail->Password = "password";

    $mail->SetFrom("[email protected]", "Any Thing");

    $mail->Subject = $subject;
    $mail->Body = $body;

    $mail->AddAddress($to);
    $mail->Send();
    unset($mail);
}

I use PHPMailer with this function...

function email($to, $subject, $body){
    require_once("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465;
    $mail->Username = "[email protected]";
    $mail->Password = "password";

    $mail->SetFrom("[email protected]", "Any Thing");

    $mail->Subject = $subject;
    $mail->Body = $body;

    $mail->AddAddress($to);
    $mail->Send();
    unset($mail);
}
昇り龍 2024-10-15 20:39:07

经过一些阅读后,我意识到我根本不需要邮件库。我正在使用 PHP 的 mail() 函数来完成我想要的任务,通过 sendmail 发送表单邮件并让 Google Apps 处理所有域电子邮件。这是对我有用的相关代码。

// Define message variables
if(get_magic_quotes_gpc()){
  $name = stripslashes($_POST['name']);
  $email = stripslashes($_POST['email']);
  $body = stripslashes($_POST['body']);
}else{
  $name = $_POST['name'];
  $email = $_POST['email'];
  $body = $_POST['body'];
}
$subject = "Website Contact Form";
$recipient = "[email protected]";
$content = "NAME: $name, $email\nCOMMENT: $body\n";

$mailheader = "MIME-Version: 1.0\r\n";
$mailheader .= "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Bcc: [email protected]" . "\r\n";

mail($recipient, $subject, $content, $mailheader) or die("Failure");
header("Location:/thankyou.php");
}

这对我来说非常有效。希望它对其他人有帮助。

After doing some reading, I realized that I didn't need a mail library at all. I'm using PHP's mail() function to accomplish exactly what I wanted, sending form mail through sendmail and having Google Apps handle all domain email. Here's the relevant code that's working for me.

// Define message variables
if(get_magic_quotes_gpc()){
  $name = stripslashes($_POST['name']);
  $email = stripslashes($_POST['email']);
  $body = stripslashes($_POST['body']);
}else{
  $name = $_POST['name'];
  $email = $_POST['email'];
  $body = $_POST['body'];
}
$subject = "Website Contact Form";
$recipient = "[email protected]";
$content = "NAME: $name, $email\nCOMMENT: $body\n";

$mailheader = "MIME-Version: 1.0\r\n";
$mailheader .= "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Bcc: [email protected]" . "\r\n";

mail($recipient, $subject, $content, $mailheader) or die("Failure");
header("Location:/thankyou.php");
}

This is working perfectly for me. Hope it helps someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文