Sending emails with PHPMailer

允我心安 提交于 2021-02-05 09:21:57

问题


I'm struggling to understand how to send emails with PHPMailer.

I have multiple website clients and I have implemented a contact form for each of them. I can send emails to my own email address but not to theirs.

This is the function I wrote to do it.

function sendSMTP($host, $usr, $pwd, $port, $fromAddr, $fromName, 
$replyAddr, $replyName, $addr, $subject, $body)
{
$mail = new PHPMailer(true);

$mail->SMTPDebug  = 1; 

$mail->isSMTP();
$mail->Host         = $host;
$mail->SMTPAuth     = true;
$mail->Username     = $usr;
$mail->Password     = $pwd;
$mail->SMTPSecure   = 'ssl';
$mail->Port         = $port;

$mail->setFrom($fromAddr, $fromName);
$mail->addReplyTo($replyAddr, $replyName);

$mail->addAddress($addr);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body    = $body;
}

I have a feeling it's not working because of something to do with the first 4 parameters (host, user, password, port). I started this months ago and sat on it and now I'm totally lost on how to fix it.

Do I have to authenticate various sender accounts for each client? I'm stuck. What is the correct way to use this function?


回答1:


I successfully send email to my or other email address from my localhost using PHPMailer. if using gmail set HOST = smtp.gmail.com, set your email address and password in USER, PASSWORD. I found the PORT number by google 'gmail port number'. Hope it will be Helpful.

<?php

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';              // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                     // Enable SMTP authentication
$mail->Username = 'example@gmail.com'; // your email id
$mail->Password = 'password'; // your password
$mail->SMTPSecure = 'tls';                  
$mail->Port = 587;     //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('sendfrom@gmail.com', 'Name');
$mail->addAddress('sendto@yahoo.com');   // Add a recipient
$mail->isHTML(true);  // Set email format to HTML

$bodyContent = '<h1>HeY!,</h1>';
$bodyContent .= '<p>This is a email that Radhika send you From LocalHost using PHPMailer</p>';
$mail->Subject = 'Email from Localhost by Radhika';
$mail->Body    = $bodyContent;
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

?>


来源:https://stackoverflow.com/questions/44872078/sending-emails-with-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!