【问题标题】:PHP code to send Email with Smtp使用 Smtp 发送电子邮件的 PHP 代码
【发布时间】:2015-06-16 11:53:15
【问题描述】:

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

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

调用php文件的简单HTML文件是 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 服务器(测试服务器)发送电子邮件,但我无法发送请帮助我或让我知道我缺少什么。

谢谢

【问题讨论】:

  • 它可以是任意数量的东西,但 SO 语法高亮应该可以帮助您在倒数第二行中发现不匹配的引号
  • 对不起,史蒂夫,你能帮我解决这个问题吗?
  • 首先修复引用 ...your email message."); 应该是 ...your email message.'); 注意单引号,而不是双引号
  • 这能回答你的问题吗? How to send email with SMTP in php

标签: php


【解决方案1】:

试试下面的代码:

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适当替换为您的

【讨论】:

  • 从哪里获取 $host、$port、$username 和 $password 的信息(是否来自 php.ini)
  • @Jibran 您可以在那里替换或设置静态变量。 $host='in.mailjet.com', $port = '25' OR '456' OR '587', $username = '你的邮箱' and $password = '你的密码'
  • " 仍然是相同的结果,只是一个空白页。我将 email.php 文件保存在 PHPMailer 下。更改了 php.ini 设置但结果仍然相同。在 IIS 服务器中运行此脚本还缺少什么"。
  • @Jibran 无需更改 php.ini 代码上面的代码对我来说工作正常。你应该跟踪错误。
  • 你的 smtp 的 php.ini 设置是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2013-09-19
  • 2015-10-22
  • 2016-04-17
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多