PHP脚本不会发送到Gmail,但其他电子邮件也可以

时间:2017-04-21 04:50:16

标签: php

我创建了一个链接到带有表单的HTML文件的PHP脚本。我将我的Gmail电子邮件地址放在我的PHP文件中,不会发送给它。它甚至不在我的垃圾邮件文件夹中。我使用GoDaddy作为我的托管服务提供商。我将PHP文件中的电子邮件更改为AOL地址,并且按照我的意图运行。我的教授给了我他的电子邮件,然后发给了他。他们有来自GroupWise的电子邮件。我打电话给GoDaddy他们说他们可以看到我的表单被提交到他们的服务器,但Google正在放弃它。下面是我的PHP代码:

<?php
$myname = $_POST['name'];
$myemail = $_POST['email'];
$mytelephone = $_POST['telephone'];
$what_service = $_POST['service'];
$mycomments = $_POST['comments'];

$to = 'example@gmail.com';
$subject = 'Contact Us - My Company Example';
$msg = "Name: $myname\n" .
"Service: $what_service\n" .
"Telephone #: $mytelephone\n" .
"Comments: $mycomments";
mail ($to, $subject, $msg, 'From:' . $myemail);

echo '<p>Thank you for contacting us!</p>';
echo 'Your Name: ' . $myname . '<br>';
echo 'Your E-Mail: ' . $myemail . '<br>';
echo 'Your Telephone: ' . $mytelephone . '<br>';
echo 'Your Service: ' . $what_service . '<br>';
echo 'Your Comments: ' . $mycomments;
?>

1 个答案:

答案 0 :(得分:0)

您必须定义SMTP协议设置才能将邮件发送到Gmail ...

以下是使用PHP PEAR

执行此操作的方法
// Pear Mail Library
require_once "Mail.php";

$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'your@gmail.com', //your gmail account
        'password' => 'snip' // your password
    ));

// Send the mail
$mail = $smtp->send($to, $headers, $body);

如果您使用gmail SMTP,请记住[在您的Gmail帐户中启用SMTP],在设置