Send email via SMTP gmail using php - php

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
}

Related

Php Mailer Ending Up In Spam In Proudction

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;

send mail from local server using xampp in php

<?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;
}
?>

PHP Mailer Multiple email

I have a form in my website. When the user fills out the form, I want an email to be sent to me with data entered in the form and also a thank you email for the person filling the form.
This is the code I am using:
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress('data#domain.org');
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress(''.$_POST['emailAddr'].''); // **
emailAddr is the name for email field in the form and I wish to send email to this email address.
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>

Php mailer body is empty

Iam using PHP mailer to send an Email the message sent but email body is Empty
here is my code
foreach($results as $result)
{
$email = $result['email'];
$body = 'Hello'.$result['username'].' we remind you to return the book to library after' .$result['days'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.myhost.com;';
$mail->SMTPAuth = true;
$mail->Username = 'send#myhost.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'send#myhost.com';
$mail->FromName ='Bookstore';
$mail->addAddress($email, 'username');
$mail->Subject = 'Book returned Time';
$mail->Body = $body;
$mail->IsHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo; return $user ;
} else {
echo 'done';
}
}

phpmailer Error after sending Email

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");

Categories