使用PHP将邮件发送到我的电子邮件帐户

时间:2014-06-13 08:17:30

标签: php sendmail

我尝试使用PHP发送邮件,但我没有成功。你能否看到代码并突出我的错误。我在ipage.com上托管我的网站

代码是

<?php

$username = $_POST['userName'];
$useremail = $_POST['userEmail'];
$userphone = $_POST['userPhone'];
$usermsg = $_POST['userMsg'];

 $host = "smtp.ipage.com";
 $username = "prashanth@pe***ll.in";
 $password = "******";

if(_POST)
{
    $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

    $smtp->send("prashanth@petrawill.in",$usermsg,$useremail);
    header('location: contact.html');
}

else
{
    echo "This might be the worst piece of code that I have written";
}

?>

4 个答案:

答案 0 :(得分:1)

我认为Mail已经初始化了。所以

if (_POST)

应该是

if ($_POST)

但我宁愿看到:

if (isset($_POST)) {

答案 1 :(得分:0)

类型_POST将为$_POST,如果您想查看其post请求,请使用此

if($_SERVER['REQUEST_METHOD'] == 'POST') {
   $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

    $smtp->send("prashanth@petrawill.in",$usermsg,$useremail);
    header('location: contact.html');
}

如果邮件课程不起作用,请不要将您的实际凭证公开给世界

答案 2 :(得分:0)

如果您在条件中使用isset,那么只会执行该块

if($_POST)
{
$smtp = Mail::factory('smtp',
array ('host' => $host,
 'auth' => true,
 'username' => $username,
 'password' => $password));

$smtp->send("prashanth@petrawill.in",$usermsg,$useremail);
header('location: contact.html');
}

答案 3 :(得分:-1)

我个人使用PHPMailer您可以在GitHub上找到该库:https://github.com/Synchro/PHPMailer

我用来做的代码如下:

<?php
require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>
相关问题