<?php
class EmailModel extends CI_Model{
public function email(){
require 'Email Files/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'Google account username';
$mail->Password = 'Google account Password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->addAddress('abc#gmail.com');
$mail->setFrom('abc#gmail.com');
$mail->Subject = 'Test';
$mail->Body = 'Testing';
if($mail->send()) {
echo 'Message has been sent';
} else {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
?>
I am using Github library for php email.
when I var_dump($mail) ; its showing all the data in the array.
now, this code is showing error: "SMTP connect() failed"
I also had this problem.Do the following steps
Go to myaccount.google.com
Click "connected apps & sites",
Turn "Allow less secure apps" to "ON" (bottom of the page).
Just I checked up with your code no error in that. you've to enable https://www.google.com/settings/security/lesssecureapps
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'test#gmail.com';
$mail->Password = 'test#12123#';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->addAddress('abc#gmail.com');
$mail->setFrom('abc#gmail.com');
$mail->Subject = 'Test';
$mail->Body = 'Testing';
if($mail->send()) {
echo 'Message has been sent';
} else {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Related
I have made a website on my localhost. After everything was ready to go I hosted the website. However, now PHPMailer seems to cause the trouble. Mails are not sending and it seems like PHP code does not execute after $mail = new PHPMailer command. Here is part of the code:
if($conn->query($sql)){
require_once '/home/ziptie/public_html/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = true;
$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 = 'mygmail#gmail.com'; // your email id
$mail->Password = 'mypassword'; // your password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('mygmail#gmail.com', 'ZIPTIE');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$v="http://ziptie.rs/verification-page.php?sifra=$token";
$body = file_get_contents('/home/ziptie/public_html/verificationemail.html');
$body = str_replace('promenljiva', $v, $body);
$mail->Subject = 'Verifikacija kupca, ZIPTIE';
$mail->Body = $body;
$mail->addAttachment("naruzbine ".date("d-m-Y").".zip");
$mail->send();
header("Refresh:0; url=https://www.ziptie.rs/verification-page.php");
//echo '<script>alert("Verifikacioni link je poslat, proverite Vašu mejl adresu.")</script>';
/*if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}*/
}
else{
echo $conn->error;
}
Does anybody know the solution? Thanks!
found solution in upgrading to PHPMailer 6.3
Here is my code below. But it keeps ending up in spam
I already added authentication and changed the setfrom address but it still ends up in spam
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = true;
// $mail->SMTPAutoTLS = true;
$mail->Username = '******'; // SMTP username
$mail->Password = "******";
$mail->Port = 25;
$mail->setFrom('info#canbeltech.com', 'Canbel Tech');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Canbel Tech IMT Biometric Card Receipt';
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
exit;
I got this error
Message could not be sent. Mailer Error: SMTP connect() failed.https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '*****';
$mail->Password = '*****';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SetFrom('*****', 'Innovatory');
$mail->AddAddress($f1_email);
$mail->isHTML(true);
$bodyContent = '<br><h3>From: '.$f1_fname.'</h3>';
$bodyContent .= '<b>Company Name: '.$f1_subject.'<br>Mobile: '.$f1_phone.'<br>Email: '.$f1_email.'</b><br><br><b>Service:</b> '.$f1_message;
$mail->Subject = 'Contact Email';
$mail->Body = $bodyContent;
$mail->mailer="smtp";
if(!$mail->send())
{
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
?>
<script>
alert('Mail has been sent.');
window.location.href='index.php';
</script>
<?php
}
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
require_once 'PHPMailer-master/class.phpmaileroauthgoogle.php';
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
//$mail->SMTPDebug =2;
$mail->Host = 'smtp.gmail.com';
$mail->Username = 'zhaider113#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$from = 'shahghafoor439#gmail.com';
$mail->setFrom($from, 'Ghafoor Shah');
$mail->addReplyTo($from, 'Ghafoor Shah');
$mail->addAddress('zhaider113#gmail.com', 'zeeshan');
$mail->Subject = 'This is subject';
$mail->Body = 'This is the body of email';
$mail->AltBody = 'This is the body of email';
$mail->send();
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
?>
I try to send email but it not send and give error message which is:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting */
try with $mail->SMTPSecure = 'tls';
See full example at PHPMailer's gmail example below: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
Make sure you include correct PHPMailer Library.
And make this changes
Some Servers not response for SSL(Secure). So that change this $mail->SMTPSecure = 'tls';
And In your code you have two Mail sending option $mail->send();
//$mail->send();//Comment this
if (!$mail->send()) {
echo 'Messag could not send';
echo 'Mailer error:' . $mail->ErrorInfo;
} else {
echo 'mail hasbeen send';
}
Now either mail sent Or either It will print Error log
Code: -
$mail = new PHPMailer();
$body="<b>This mail is sent using PHP Mailer</b>";#HTML tags can be included
$mail->IsSMTP();
$mail->SMTPAuth = true; #enable SMTP authentication
$mail->SMTPSecure = "tls"; #sets the prefix to the server
$mail->Host = "smtp.gmail.com"; #sets GMAIL as the SMTP server
$mail->Port = 587; #set the SMTP port
$mail->Username = "morelifelondon#gmail.com"; #your gmail username
$mail->Password = "***********"; #Your gmail password
$mail->From = $email;
$mail->FromName = $name ;
$mail->Subject = "Enquiry";
$mail->Body = $messages;
$mail->AddAddress("morelifelondon#gmail.com","contact");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Mail has Been Sent.';
}
}
I want that morelifelondon#gmail.com is replaced by abhishek#gmail.com. Is this possible ?
$mail->AddAddress("abhishek#gmail.com","contact");
or use
$mail->AddCC("abhishek#gmail.com","contact");