PHP send mail error 3 - php

When I try to send the form on my web site, I got this error:
Sorry ..., it seems that my mail server is not responding. Please try again later!
I thing that is a problem with me contact_me.php file
There is the code
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
error_reporting(E_STRICT);
date_default_timezone_set('Portugal/Lisbon');
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "a.a.pt"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "a.a.pt"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "a#a.pt"; // SMTP account username
$mail->Password = "a"; // SMTP account password
$mail->SetFrom('a#a.pt', 'First Last');
$mail->AddReplyTo("a#a.pt","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$address = "a#a.pt";
$mail->AddAddress($address, "a#a.pt");
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$formcontent= eregi_replace("[\]",'',$formcontent);
$mail->MsgHTML($formcontent);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

You can try using my class, i'm sure this runs 100%, even if it does not work for you is a credential problem or email server that for some reason waste for the connection
class Mailplus{
protected $host = "ssl://smtps.*.com";
protected $port = "123";
protected $username = "email#example";
protected $password = "*";
protected $from = "Email from - Name <email#example>";
protected $smtp;
function __construct() {
require_once "Mail.php";
$this->smtp = Mail::factory('smtp',
array ('host' => $this->host,
'port' => $this->port,
'auth' => true,
'username' => $this->username,
'password' => $this->password));
}
function SendMail($to ,$subject,$body){
$headers = array ('From' => $this->from,
'Subject' => $subject,
'MIME-Version' =>'1.0',
'Content-Type' =>'text/html',
'charset' => 'ISO-8859-1');
$mail = $this->smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
return "Ex_001";
} else {
return true;
}
}
}
Then use this comand
$mail = new Mailplus ();
if ($mail.SendMail("to#example.com","Subject","html emial")==ture){
//email sent
else{
//email not sent
}

Related

phpmailer - email not send in gmail

I am having problem with sending mail using phpmailer. I am a beginner programmer. I am trying to make contact form. My code is as follow(submit.php). Please suggest me.. Thanks in advance .
session_start();
require_once 'libs/phpmail/PHPMailerAutoload.php';
$errors = array();
if(isset($_POST['name'], $_POST['phone'],$_POST['mail'],$_POST['message'])){
$fields = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
'email' => $_POST['mail'],
'message' => $_POST['message']
);
foreach ($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The '. $field . ' field is required';
}
}
if(empty($errors)){
$m = new PHPMailer;
$m -> isSMTP();
$m -> SMTPAuth = true;
//$m -> SMTPDebug = 2;
$m -> Host = 'smtp.gmail.com';
$m -> Username = 'xxxx#gmail.com';
$m -> Password = 'xxxx';
$m -> SMTPSecure = 'ssl';
$m -> Port = 465;
$m -> isHTML();
$m -> Subject = 'Contact form submitted';
$m -> Body = 'From: ' . $fields['name']. '('. $fields['phone'] . $fields['email']. ')'.'<p>' .$fields['message'] .'</p> ';
$m -> FromName = 'Contact';
// $m ->addReplyTo($fields['email'], $fields['name']);
$m -> addAddress('ssss#gmail.com', 'xxxxxxxx');
if($m->send()){
header('Location: thanks.php');
die();
}else{
$errors[] = 'Sorry could not send email. Please try again';
}
}
}else{
$errors[] = 'some thing went wrong';
}
$_SESSION['error'] = $errors;
$_SESSION['field'] = $fields;
header('Location: form.php');
My setting phpmailer, everything works
function __construct ($to, $subject, $body) {
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->CharSet = 'UTF-8';
//Set the hostname of the mail server
$mail->Host = "mail.xxxxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->AuthType = 'PLAIN';
//Username to use for SMTP authentication
$mail->Username = "xxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxx";
//Set who the message is to be sent from
$mail->setFrom('erp#xxxxxx.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$this->mail = $mail;
}
function SendMail () {
//send the message, check for errors
if (!$this->mail->send()) {
return "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
// using
$email = $this->request->getPost('email');
$smtp = new \SmtpClient($email, 'Test', $template);
$result = $smtp->SendMail();
Remove
$m -> Subject = 'Contact form submitted';
And try again.
When I remove the subject it worked.

how to variable pass mail function in php

I am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);

How can i send an email using php including mail.php file

I want to send an email in php from my localhost which includes mail.php file.
Here is my code for test.php file:
<?php
include_once("Mail.php");
$From = "Sender's name <testing123xyz#gmail.com>";
$To = "Recipient's name <test2#gmail.com>";
$Subject = "Send Email using SMTP authentication";
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication";
$Host = "mail.gmail.com";
$Username = "testing123xyz";
$Password = "testing";
// Do not change bellow
$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject);
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true,
'username' => $Username, 'password' => $Password));
$mail = $SMTP->send($To, $Headers, $Message);
if (PEAR::isError($mail)){
echo($mail->getMessage());
} else {
echo("Email Message sent!");
}
?>
When i hit the url of test.php file, server is throwing following error :
Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: No connection could be made because the target machine actively refused it. (code: -1, response: )]
Please help me out.
Thanks.
Download and use phpmailer from github:https://github.com/PHPMailer/PHPMailer
Also, check this tutorial, it will help you: http://phpmailer.worxware.com/?pg=tutorial
This is the php code to send emails:
<?php
function email($recipient_email_id,$senders_name){
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$webmaster_email = "reply#gmail.com"; //Reply to this email ID
$email = "$recipient_email_id"; // Recipients email ID
$name = "$senders_name"; // sender's's name
$mail->From = $webmaster_email;
$mail->FromName = $name;
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
My guess is that you are using localhost. Try using Test Mail Server
EDIT as of this link, you are using wrong host. Change it to smtp.gmail.com

Send mail via SMTP?

I have a form on my website where people can join events. The code behind it works in this way:
All info is saved in a database. This part works fine.
The second part of the code send out an email to me and to the user with the info he entered (same info as saved in the database)
The issue is that these emails are sent unauthenticated through a default email on the hosting account. I had to modify the script to force SMTP authentication with a valid email under my hosting account to fix the error. Right now the script sends out the email but it ends in spamfilter with all ISPs so the user never receive the email.
I have no idea of how to do, or create the codes so the script use SMTP authentication. Below is the codes I have so fare. Can someone help me?
<?
// SEND OUT EMAIL PART
// COPY SEND TO MY SELF
$to = "my#email.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$fields = array();
$fields{"name"} = "Name";
$fields{"address"} = "Address";
$fields{"phone"} = "Phone";
$fields{"email"} = "E-mail addesse";
$body = "INFO:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// SEND TO THE USER
$headers2 = "From: my#email.com";
$subject2 = "THANKS!";
$fields2 = array();
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Thanks!";
$body2 = "
TEXT TO EMAIL RECEIVER
\n\n"; foreach ($fields2 as $a => $b){ $body2 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// ERROR MESSAGES
if($from == '') {print "MISSING EMAIL ADDRESS.";}
else {
if($name == '') {print "MISSING NAME";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $body2, $headers2);
if($send)
{header( "Location: http://mysite/send.php" );}
else
{print "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; }
}
}
?>
Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:
// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->Port = 465; // change port is required
This is one way to do it. You need to include PHPMailer
#somewhere in code before you call function
include($path_to_PHPMailer."class.smtp.php");
function sendEmail($email,$name,$title,$content,$who=false,$att=array('')){
//VARIABLE $email is email of sender
//VARIABLE $name is name of sender
//VARIABLE $title is title of email
//VARIABLE $content is content of email
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
// DOUBLE $mail->Host = "your email_server"; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "your email_server"; // sets the SMTP server
$mail->Port = server port; // set the SMTP port for the GMAIL server
$mail->Username = "your username"; // SMTP account username
$mail->Password = "your password"; // SMTP account password
if($who){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$address = "set your email";//EMAIL OF RECIPENT
} else{
$mail->SetFrom("set your email", "your name (or service name)");
$mail->AddReplyTo("set your email","your name (or service name)");
$address = $email;
}
$content=str_replace("\n", "<br>", $content);
$mail->Subject = $title;
$mail->MsgHTML($content);
$mail->AddAddress($address, $name);
$mail->CharSet='utf-8';
if($att[0]!=''){
foreach ($att as $at){
$mail->AddAttachment($at);
}
}
if(!$mail->Send()) {
return false; //$mail->ErrorInfo;
} else {
return true;
}
}

Two versions of same email to two different recipients using class.phpmailer.php

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.

Categories