使用Smtp发送电子邮件的PHP代码

时间:2015-06-16 11:53:16

标签: php

我是PHP新手并尝试使用PHP脚本发送电子邮件。

我确实将“ php.ini ”设置更改为         SMTP = "in.mailjet.com" smtp_port= 25 auth_username="abc" auth_password="abc" sendmail_from = "abc@abc.com"

简单的 HTML 文件来调用php文件       form name="contactform" method="post" action="PHPMailer/email.php"
style="padding-left:5%;"> <td colspan="2" style="text-align:center"> <br> <input class="btn btn-info" type="submit" value="Download">

email.php 文件

    require 'class.phpmailer.php';
    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->Host="in.mailjet.com";
    $mail->SMTPAuth = true;
    $mail->Username = 'abc';
    $mail->Password = 'abc';
    $mail->SetFrom('abc','Your Name');
    $mail->AddAddress('jibran.ishtiaq@runyourfleet.com','Their Name');
    $mail->Subject = 'Your email from PHPMailer and Mailjet';
    $mail->MsgHTML('This is your email message.");
    $mail->Send();

我正在尝试从IIS服务器(测试服务器)发送电子邮件,但我无法发送请帮助我或让我知道我错过了什么。

谢谢

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

require 'class.phpmailer.php';

$mail = new PHPMailer();

$mail->isSMTP(); // send via SMTP

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;

$mail->Host = $host; //SMTP server
$mail->Port = $port; //set the SMTP port; 587 for TLS
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail

if (strlen($username)) {
    $mail->SMTPAuth = true;
    $mail->Username = $username;
    $mail->Password = $password;
} else {
    $mail->SMTPAuth = false;
}

$mail->From = $from; // FROM EMAIL
$mail->FromName = $fromName; // FROM NAME
$mail->addAddress($to);
$mail->addReplyTo($from);

$mail->IsHTML($html);

$mail->Subject = $subject; // YOUR SUBJECT
$mail->Body = $message; // YOUR BODY
$mail->addAttachment($attachment); // YOUR ATTACHMENT FILE PATH

if ($mail->send()) {
    echo "Message Sent";exit;
} else {
    echo "Message Not Sent<br>";
    echo "Mailer Error: " . $mail->ErrorInfo;exit;
}  

注意:适当地替换所有variables