如何将电子邮件发送到我的 Gmail 而不是托管


How to send an email to my gmail rather than hosting

所以我从头开始建立自己的网站,我想创建一个电子邮件功能。现在我想这样做,以便用户不必使用 Outlook 或其他任何东西来向我发送电子邮件,我希望他们可以通过单击按钮将其发送给我。

现在这基本上是我现在使用的要点

mail($to, $subject, $body);

但是,这仅在我将电子邮件设置为托管服务器电子邮件(byethost)时才有效。我尝试将其设置为我的gmail帐户,但这不起作用。

那么,我该怎么做才能让它向我的gmail发送电子邮件

您需要 SMTP 才能使用 Gmail 发送邮件。PHPMailer是我经常用来做这件事的人。

https://github.com/PHPMailer/PHPMailer

您说您在托管服务器电子邮件(byethost)中收到邮件,但在gmail帐户中没有收到邮件。但是你应该在gmail中收到邮件。你会签入你的垃圾邮件文件夹吗?如果您使用正确的电子邮件标题发送邮件,则应将其放入您的收件箱。检查这里完整的邮件头

为了避免服务器配置问题,您可以使用PHP邮件程序。您还可以使用 this

>

我在其中一个项目中遇到了类似的情况,其中用户(站点访问者)的查询将发送给站点管理员(gmail帐户)。已经用Ajax调用发送表单上收集的详细信息,发布到使用Postfix发送邮件的php文件中。(该网站托管在Ubuntu服务器上)。

页面中的 JQuery 代码如下所示。

$("#btn_email").click(function () {
            //get input field values data to be sent to server
            post_data = {
                'vemail': $('input[name=email]').val(),
                'query'  : $('input[name=query]').val(),
                'subject': 'New Query',
                'msg': 'There is new request for query'
            };
            //Ajax post data to server
            $.post('sendmail.php', post_data, function (response) {
                if (response.type == 'error') { //load json data from server and output message
                    output = '<div class="error">' + response.text + '</div>';
                } else {
                    output = '<div class="success">' + response.text + '</div>'; }}});

将请求处理为 的 PHP 代码

$email=$_POST['email'];
$subject = 'New Query';
$headers = 'From:'. $email . "'r'n"; // Sender's Email
$message = $_POST['msg']. "'r'n" .$_POST['query'] . "'r'n'r'nEmail : " . $email ;
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
$send_mail = mail("mycontact@gmail.com", $subject, $message, $headers); 
if(!$send_mail)
{
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi, We have added '.$user_email .' to the query. Will answer you soon. Thank you for your interest.'));
    die($output);
}
}

如果只是为了好玩,您可以考虑的一种选择是使用 Sengrid,它非常简单有效。在 byethost email() 函数中不起作用。获得APIKEY后,您可以轻松设置电子邮件。这是来自github的php代码:

$sendgrid = new SendGrid('YOUR_SENDGRID_APIKEY');
$email = new SendGrid'Email();
$email
    ->addTo('foo@bar.com')
    ->setFrom('me@bar.com')
    ->setSubject('Subject goes here')
    ->setText('Hello World!')
    ->setHtml('<strong>Hello World!</strong>')
;
$sendgrid->send($email);
// Or catch the error
try {
    $sendgrid->send($email);
} catch('SendGrid'Exception $e) {
    echo $e->getCode();
    foreach($e->getErrors() as $er) {
        echo $er;
    }
}

而且您有一个免费计划,每月有足够的免费电子邮件。