Hi I am trying to test my PHP mail function but it is not working. I have shared hosting all of my website mail function are not working
<?PHP
$sender = 'xx#gmail.com';
$recipient = 'xx#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
I have also checked my php.ini if mail function is disabled but it is not disabled
disable_functions =
I also tried using Easy WP SMTP Plugin. But it is not working. I am sure that my settings are correct.
But this is what i got
SMTP ERROR: Failed to connect to server: Connection timed out (110)SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Thank you
Related
We're struggling with email submission via PHP. Currently we're using the following code:
<?php
ini_set('display_errors',1);
ini_set("SMTP","smtp.gmail.com");
ini_set("smtp_port","587");
ini_set("auth_username","mymail_address#gmail.com");
ini_set("auth_password","mypassword");
$to = 'mydestination_address#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail_address#gmail.com';
if(mail($to, $subject, $message, $headers))
{
echo "Success!";
}else
{
echo "Failed.";
}
?>
The outcome of the code gives the following message:
Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS
command first. e22sm16110599edu.35 - gsmtp in
C:\inetpub\production\mailtest.php on line 13 failed.
Please note that our application has been created with PHP using IIS.
I think you can do it with:
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
Please check if your PHP installation has SSL support before use the code above.
i will send mail using php mail function but it can display some error..
Warning: mail(): Failed to connect to mail server at
"localhost" port 25, verify your "SMTP" and
"smtp_port" setting in php.ini or use ini_set() in
mail.php
<?PHP
$sender = 'sender123#gmail.com';
$recipient = 'resever123#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
If you are testing on your localhost you most likely dont have an SMTP setup. You have to setup an smtp connection for php to send the message.
I would suggest using something like phpmailer which makes it easier when working on local testing servers.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I wish to know the difference between mail sending using localhost and live server.
This is my code and someone help to correct this code so that mail along with attachment can be sent successfully.it showed mail sent but I did not receive any.I am not sure where the error or code missing happens.Help me so that I can rectify it.
//This is m1.php file
<?php
$max_no_img=1; // Maximum number of images value to be set here
echo "<form method=post action=m2.php enctype='multipart/form-data'>";
echo "<tr><td>Photo: </td><td><input type='file' name='uploadfile' class='bginput'></td></tr>";
?>
<input type="submit" name="Submit" value="Send">
//This is m2.php file.From m1.php link redirects to here and the mail with attachment to be done here
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "mymail#gmail.com"; // SMTP account username
$mail->Password = "password";
if($_POST){
$targetFolder = "image/".$_FILES['uploadfile']['name']; //My location where images stored & retrieved
copy($_FILES['uploadfile']['tmp_name'],$targetFolder);
$htmlbody = " Your Mail Content Here.... You can use html tags here...";
$to = "mymail#gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: mymail#gmail.com\r\nReply-To: mymail#gmail.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents($targetFolder)));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip;
name=\"$targetFolder\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
}
?>
mail() will return value if your mail sent successfully.
Try this.
$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
if($status) {
echo 'Mail sent successfully';
} else {
echo 'Mail sending failed';
}
The reason that you are not receiving the email is likely that you have not configured your localhost machine to run an SMTP server.
To test emails on localhost I use Antix SMTP imposter. It sits on your machine and shows emails that would have been sent had your app been connected to an SMTP server. This has the added bonus of allowing you to see the emails as they would be received without having to worry about sending live emails from your test environment.
I am trying to send mail using php.And i am using WampServer.
so i tried the following code
ini_set("SMTP","smtp.gmail.com" );
ini_set("smtp_port","465");
ini_set('sendmail_from', 'person1#gmail.com');
$to = "person2#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "person1#gmail.com";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
but it take more time to connect and says could not connect with localhost.
Please help me in solving the problem
try this configuration:
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/
this might help.
somehow, instead of "smtp.gmail.com", for me it works with "ssl:smtp.gmail.com"
This line:
ini_set("SMTP","smtp.gmail.com" );
should be
ini_set("SMTP","ssl:smtp.gmail.com" );
Also, see this response to a similar question: Send email using the GMail SMTP server from a PHP page
You're trying to send mail from your localhost (Your PC) I guess It's not setup to send mail. Move the script to a production server and it will work
I am trying to use PEAR Mail to send from my gmail address using below code,
<?php
include("Mail.php");
echo "This test mail for authentication";
try{
$from_name = "Test";
$to_name = "from name";
$subject = "hai";
$mailmsg = "Happy morning";
$From = "From: ".$from_name." <frommail#gmail.com>";
$To = "To: ".$to_name." <tomail#gmail.com>";
$recipients = "tomail#gmail.com";
$headers["From"] = $From;
$headers["To"] = $To;
$headers["Subject"] = $subject;
$headers["Reply-To"] = "gunarsekar#gmail.com";
$headers["Content-Type"] = "text/plain";
$smtpinfo["host"] = "smtp.gmail.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "mymail#gmail.com";
$smtpinfo["password"] = "mypassword";
//$smtpinfo["debug"]=True;
$mail_object =& Mail::factory("smtp", $smtpinfo);
$mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}catch(Exception $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "<br>Fin";
?>
this code not return any error or warning , it simply shows "Message successfully sent!"
but, mail not receiver to mail the address.
can any one please tell what problem in mycode or what actually happening.,
The first thing I see is that you have a mistake: Your check checks against an variable called $mail, but everything else refers to $mail_object. If that's in your actual code, then I'm guessing that might be part of it.
Some basic checks:
Did you check to make sure that you have POP or IMAP enabled in Gmail?
Have you set up this account with the same username and password on a normal machine, to ensure you can send and receive email outside of PHP?
Verify that you can even talk to the GMail server (that it isn't blocked for some reason) by pinging smtp.gmail.com or using telnet to open a connection to port 25: telnet smtp.gmail.com 25
Read over the Gmail help for sending email.
Beyond that, it looks like GMail requires TLS or SSL, which means you have to use port 587 or port 465. I don't know if that package can handle encrypted connections, though. (Even on port 25, GMail requires SSL encryption.) That may preclude this from working at all.