php - 如何使用 PHPMailer 发送短信?
问题描述
我想使用 phpMailer 发送短信,我尝试进行所有配置,但我收到了消息
警告:mail():SMTP 服务器响应:550 访问被拒绝 - C:\xampp\htdocs 中的 HELO 名称无效(参见 RFC2821 4.1.1.1)
以下是我的代码
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try{
$mail->isSMTP();
$mail->Host = 'mail.ku..e.com';
$mail->SMTPAuth = true;
$mail->Username = 'info@ku..e.com';
$mail->Password = 'my password';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
//$mail->Port = 25;
$to = "+25078.....@vtext.com";
$from = "info@k..e.com";
$message = "aaaaa";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name <info@ku..e.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, '', $message, $headers);
} catch (Exception $e){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
我像这样配置了 php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = mail.ku..e.com
; http://php.net/smtp-port
smtp_port=25
; For Win32 only.
; http://php.net/sendmail-from
;username = info@ku..e.com
;password = my password
;sendmail_from = info@ku..e.com
我使用 Inmotion 托管
请任何人都可以使用上述代码帮助我发送短信
解决方案
呃,你没有使用 PHPMailer!您开始使用,但随后您放弃并改用使用mail()
!替换这部分:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name <info@ku..e.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, '', $message, $headers);
和:
$mail->isHTML();
$mail->addAddress($to);
$mail->setFrom('info@ku..e.com', 'Your Name');
$mail->Body = $message;
$mail->send();
然后您将正确使用 PHPMailer,并按照我假设的方式通过 SMTP 发送。
就发送 SMS 而言,PHPMailer 对此没有意见 - 如果您有可靠的电子邮件到 SMS 网关,那么它应该可以正常工作。也就是说,您的 SMS 网关不太可能需要 HTML 输入,因此您可能需要删除该$mail->isHTML();
行以使其作为文本/纯文本发送,并且您可能也希望使用$mail->CharSet = 'UTF-8';
(并确保您的内容是 UTF-8 格式)。
推荐阅读
- java - 如何从数组列表中添加/删除多个对象
- react-native - 无法从 android 模拟器获取填充的 sqlite db
- openid-connect - 我们应该比较 Access Token 和 ID Token 中的“sub”声明吗?
- java - 复制类但覆盖现有的类导入
- python - “计数器”对象没有属性“计数”
- ruby-on-rails - Ruby Google Cloud PubSub 异步拉取返回 GRPC::DeadlineExceeded 错误
- python - Django按日历日期而不是发布日期订购事件
- c# - 在圆的边缘移动对象
- database - 无法更新 Mongoose 模型,.findByIdAndUpdate() 不起作用
- c++ - 如何在二维列表中插入元素,如向量?