i am using phpmailer and i am getting following error:
Message was not sent
Mailer Error: SMTP Error: Could not connect to SMTP host.
<?php
require("class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'ssl://smtp.myhost.com:465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = 'myemail#myhost.com';
$mailer->Password = 'mypass';
$mailer->From = 'myemailagain#myhost.com';
$mailer->FromName = 'myname';
$email1 = $_GET['email'];
$verification = rand();
$mailer->Body = 'Welcome to our site';
$mailer->Subject = 'verification';
$mailer->AddAddress($email1);
if(!$mailer->Send())
{
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
note: i use "myhost.com" but it's not my real domain
I believe you are specifying your host incorrectly. Try this instead:
$mailer->Host="smtp.myhost.com";
$mailer->Port=465;
$mailer->SMTPSecure="ssl"; //If this doesn't work, try 'tls'
If it still doesn't work, consider setting:
$mailer->SMTPDebug=1;
Also, make sure PHP's OpenSSL extension is enabled.
Related
I am trying to send emails using PHPMailer using GoDaddy's SMTP servers (since I hear they blocked Gmail's SMTP or else I would be using that). Here is my code:
<?php
require_once "vendor/autoload.php";
$name = $_POST["name"];
$email_from = $_POST["email"];
$telephone = $_POST["telephone"];
$message = "Name: ".$name."\r\n".
"Email: ".$email_from."\r\n".
"Telephone: ".$telephone."\r\n";
$email_to = "myemail#gmail.com";
if(isset($_POST['submit'])){
$mail = new PHPMailer\PHPMailer\PHPMailer;
$mail->isSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = false;
$mail->Port = 50;
$mail->From = $email_from;
$mail->FromName = $name;
$mail->addAddress($email_to);
$mail->addReplyTo($email_from);
$mail->Subject = "Info";
$mail->Body = $message;
if ($_POST["submit"]){
if(!$mail->send()){
echo "Mailer error: " . $mail->ErrorInfo;
}
else{
echo "Message sent successfully";
}
}
}
I have looked up so many solutions but I couldn't find anything that helps. Basically non of the GoDaddy SMTP configurations I've tried worked. I get the error: Mailer error: SMTP connect() failed. https://github.com/PHPMailer/wiki/Troubleshooting.
How should I configure this so it works? Thanks.
I am using the code below for the mailing list of the website I am working on, and it's working fine on my localhost using my credentials and on some other server using some other credentials were given to me.
<?php
if(isset($_POST['submit']))
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$query = "SELECT * FROM mailing_list";
$result = mysqli_query($connect, $query)
or die('Error querying the database!');
$mail->IsSMTP();
$mail->Host= "smtp";
$mail->Username= "some email address was given to me";
$mail->Password= "password";
$mail->SMTPAuth= true;
$mail->SMTPSecure = 'tls';
$mail->From ="some email address was given to me";
$mail->FromName ="CSCA";
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$to = $row['email'];
$mail->ClearAddresses();
$mail->AddAddress($to);
$mail->Subject= $_POST['subject'];
$mail->Body = "Dear $first_name $last_name,\n" . $_POST['body'];
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Now when I use the following code for the contact us page of the same website it works fine one my localhost using my own credentials, but it doesn't work on the other server I used above. I got a message in the spam 'mail delivery failed'. I don't know how with the same credentials you can send mail but you can't receive! I mean with the same server and same credentials you the mailing list works fine but the contact us form gives an error. Is there a way to fix that??
Thanks in advance!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$name = $_REQUEST['name'];
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$mail->IsSMTP();
$mail->Host="smtp";
$mail->Username="some email address was given to me";
$mail->Password="password";
$mail->SMTPAuth=true;
$mail->SMTPSecure = 'tls';
$mail->From =$email;
$mail->FromName =$name;
$mail->AddAddress("some email address was given to me", "Some name");
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
I'm trying to send two versions of the same email to two recipients using phpMailer
Depending on the email I need to send one version or another.
At the moment I'm able to send only the same version to both email address
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx#xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx#xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first#email.xxx", "second#email.xxx");
foreach($add as $address) {
if($address == "first#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first#mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.
I think you want to seach the $maileremail inside $add and send the desired email.
$add = array("first#email.xxx", "second#email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
EDITED:
I misunderstood the question. Brian is right.
I see most PHPMailer questions use IsSMTP(), but I'm working from a very basic example I found yesterday on SO (can't seem to find the link in History though) that doesn't.
require_once("class.phpmailer.php");
$email = new PHPMailer();
if (isset($_FILES['upload']['size']))
{
echo 'file size: '.(basename($_FILES['upload']['size'])* 1024).'<br />';
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded<br />";
$email->AddAttachment($target_path);
}
else
{
echo "There was an error uploading the file, please try again!<br /> ".basename($_FILES['upload']['error']);
}
}
else
{
echo "No file was found for the upload.<br />";
}
$email->To = "me#this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
//echo "mail built...<br />";
if (!$email->Send())
{
echo "Mailer error: " . $email->ErrorInfo;
}
I've tried sending twice. The code executes fine and the last if statement returns true.
Do I have to use IsSMTP()? If so, does it have to route through SSL like I've seen suggested here?
Thanks in advance?
EDIT
I've added details required for SMTP as follows:
$email->IsSMTP();
$email->Host = "localhost";
$email->Port = 465;
$email->SMTPAuth = true;
$email->Username = "bacon";
$email->Password = "4ndCh33se";
This doesn't seem to have triggered it to send. There also still isn't any error reported.
EDIT
Turns out there was an error, I had a redirect in a weird place that was preventing me from seeing it. The error is as follows:
You must provide at least one mailer is not supported.
Initially, the addition of the IsSMTP() seemed vital and was implemented as per other answers.
All answers failed, however, to address one missing piece of the puzzle.
PHPMailer requires at least 1 address to be added as per below. I just used the same address here as was set in the To property:
$email->To = "me#this.com";
$email->AddAddress("me#this.com");
Once this was done, all worked perfectly fine.
if you want to send mail through smtp you need to set SMTP username and password and other setting.
And also you need to add IsSMTP().
Example Gmail SMTP setup
$this->SwiftMailer->smtpType = 'tls';
$this->SwiftMailer->smtpHost = 'smtp.gmail.com';
$this->SwiftMailer->smtpPort = 587;
$this->SwiftMailer->smtpUsername = 'emailaddress#gmail.com';
$this->SwiftMailer->smtpPassword = 'gmailPassword';
$this->SwiftMailer->sendAs = 'html';
$this->SwiftMailer->from = 'sender#gmail.com';
$this->SwiftMailer->fromName = 'Sender Name';
$this->SwiftMailer->to = "receiver#anything.com";
$this->Email->sendAs = 'html';
$email->To = "me#this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
I have the following besides yours. Maybe it helps:
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = true; // turn on SMTP authentication
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password
I have changed nothing in class.phpmailer.php
Try this
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = false; // turn on SMTP authentication
$email->Port = 25
$email->SMTPSecure = "";
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password
I keep getting this error..
below is my code:
<?php
ini_set("include_path", "phpmailer/");
require 'phpmailer/class.phpmailer.php';
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->Port='465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = 'mail#gmail.com'; // Change this to your gmail adress
$mailer->Password = '****'; // Change this to your gmail password
$mailer->From = 'maile#gmail.com'; // This HAVE TO be your gmail adress
$mailer->FromName = 'Belmer'; // This is the from name in the email, you can put anything you like here
$mailer->Body = 'This is the main body of the email';
$mailer->Subject = 'This is the subject of the email';
$mailer->AddAddress('another#ymail.com'); // This is where you put the email adress of the person you want to mail
if(!$mailer->Send())
{
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
Anyone knows this error? Your reply is greatly appreciated.
Got it! Thanks guys for viewing.
There's actually no problem with the code, it has to be some settings on my local server. My solutions is i uploaded the files to my live webserver and everything is working fine.