Contact form with PHPMailser doesn't work - html

I would use PHPMailer with a contact form but I am doing something wrong because it doesn't work.
<?php
require ("class.phpmailer.php");
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$message=$_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'myemail#mail.com';
$mail-> Password = 12345;
$mail->From = ($email);
$mail->FromName = ($name);
$mail->addAddress = 'myemail#mail.com';
$mail->isHTML(false);
$mail->Subject = "Enquiry from Website submitted by $name";
}
if (!$mail->Send()) {
echo "<script>alert('Submission failed.');</script>";``
}
else {
echo "<script>alert('Email has been sent successfully.');</script>";
}
?>
Would you be so kind to give me a little big help? I don't understand where I'm doing wrong.
Thank you.

If you inspect debug output you can find a hint. Most possible reasons are incorrect parameters or maybe smtp server rejects request.
Please post debug output.

Related

SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP Connect failed

Excuse me, I really need some help for this code :
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$from = "xxx#gmail.com";
$mail = new PHPMailer();
$mail->SMTPDebug=2;
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // SMTP host
$mail->Port = 587; // set the SMTP port
$mail->Username = "xxx#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$mail->SetFrom($from , 'Admin xxx');
$mail->Subject = "mail title";
$mail->MsgHTML("Halo halo");
$address = "xxx#gmail.com";
$mail->AddAddress($address, $to);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
And I get this error: 2018-06-08 01:52:09 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2018-06-08 01:52:09 SMTP connect() failed.
I have tried all of solution about this problem, but nothing happen. please, I need your help. thank you..

Phpmailer can't send email (Could not instantiate mail function.;)

My sendmail function:
$usr = $user;
$body_msg = $this->getBodyEmail($fields);
$email_from = 'contato#mydomain.com';
$email_name = 'Name';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SetLanguage("br", "phpmailer/language/phpmailer.lang-br.php");
$mail->setFrom( $email_from, $email_name);
$mail->isHTML(true);
$mail->Subject = 'Contato';
$mail->Body = $body_msg;
$mail->addAddress($fields['email'], $fields['nome']);
Of course I hid the domain.
So I try the $mail->send(), but it will not work and the $mail->ErrorInfo; displays: "Could not instantiate mail function.".
This system used to work without SMTP, I do not need it.
EDIT:
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';
What it can be?
I changed the SetFrom to From and FromName and it seems to have worked.
$mail->From = 'contato#mydomain.com';
$mail->FromName = 'Name';

SMTP is not sending emails in gmail business account

This code is working well in gmail personal account, but when I try to use gmail business account, it is not working and keeps giving an error. 5.5.1 Authentication Required.
void SendEmail()
{
DataTable data = GetData();
DataTable email_data = GetEmailData();
data.TableName = "Employee_Data";
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(data);
using (MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
String from = "seong#abcd.net";
for (int i = 0; i < email_data.Rows.Count; i++)
{
String to = email_data.Rows[i][0].ToString();
using (MailMessage mm = new MailMessage(from, to))
{
mm.Subject = "Employees Attachment";
mm.Body = "Employees Exported Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Employees.xlsx"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "seong#abcd.net";
credentials.Password = "1234";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
}
I solved this problem.
The account should not use 2nd verification in gmail if you want to use SMTP.
https://support.google.com/accounts/answer/1064203?hl=en&ref_topic=7189195
I can't control those things, I ask the administrator allow not to use 2nd verification.
So I can work with that account using SMTP.
Smtp method for Business Gsuite
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "Enter the user ID"; // SMTP account username
// SMTP account password
$mail->Password = "Enter your password";
$mail->SetFrom('Enter the User ID', 'Subject');
$mail->AddReplyTo("Enter the User ID", "Subject");
$mail->AddAddress($to, "Name");
$mail->Subject = "Contact Enquiry";
$message = '';
$mail->MsgHTML($message);
if($mail->Send()){
$mail->ClearAddresses();
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

phpmailer does not connect to SMTP servers

i've been trying to use phpmailer and tried using live.com and gmail.com but it always can't connect to SMTP server. here's the full code (i've tried live.com using smtp.live.com but i get the same problem "Message could not be sent.Mailer Error: SMTP connect() failed.")
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 = 'my emil address'; // SMTP username
$mail->Password = 'my password'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable encryption, 'ssl' also accepted
$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('another email address'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I suspect it's because you've not set the port, and SMTPSecure should be lower case. Change this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Beyond that, check that you're allowed to send outbound mail by your ISP/firewall and that your DNS is working.
Try This:
$mail->SMTPSecure = 'tls';
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587; //You have to define the Port
$mail->SMTPDebug = 3;
Remove This:
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'TLS';

phpmailer and gmail SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed

I need help please
this is my code:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = 'some#gmail.com';
$mail->Password = 'somepass';
$mail->addAddress('another#gmail.com', 'Josh Adams');
$mail->Subject = 'PHPMailer GMail SMTP test';
$body = 'This is the HTML message body in bold!';
$mail->MsgHTML($body);
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
and I get this error:
2013-12-11 15:15:02 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed. Mailer Error: SMTP connect() failed.
any help please?
You might want to start by isolating this problem to determine whether it's truly a network problem; or whether it's specific to PHP mailer or your code. On your server, from a command prompt, try using telnet to connect to smtp.gmail.com on port 587, like so:
telnet smtp.gmail.com 587
You should see a response from smtp.gmail.com, like so:
Trying 173.194.74.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp
Do you see this, or does the connection attempt hang and eventually time out? If the connection fails, it could mean that your hosting company is blocking outgoing SMTP connections on port 587.
This worked for me:
change:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
to
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
The code below:
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Host = 'ssl://smtp.gmail.com:465';
I was facing the same issues on Godaddy hosting so I spend lots of time and fix it by disabling the SMTP restriction on the server end.
SMTP code is for PHPMailer
$mail = new PHPMailer(true);
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPDebug = 2; //Enable verbose debug output
//$mail->isSMTP(); //Send using SMTP
$mail->Host = "tls://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = contact#example.in;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('contact#example.in', 'Online Order');
$mail->addAddress('test#gmail.com'); //Add a recipient
$mail->addReplyTo('contact#example.in', 'Jewellery');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Order';
$mail->Body = 'This is test email content';
$mail->AltBody = 'This is test email content';
if($mail->send()){
return '1';
}else{
return 'Order email sending failed';
}
change
$mail->SMTPSecure = "tls";
with
$mail->SMTPSecure = 'ssl';