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.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
My web developer made this script to send notifications to people subscribing to my mailing list and the people who contact me, through the form on my website
The problem is, the recipients are not able to receive the emails, and I am not able to receive the notification on my mail id about a new subscriber or a contact
Sender id - notifications#llaveshagarwal.com
Moreover, the same script works on his server (which we used for testing) but doesn't work on my server
Is there a problem with my SMTP server settings ?
Also, I am using Google Apps for Gmail
Here is his script
What should I do ?
<?php
if($_POST['type'] == "subscribe") {
/*$to = "notifications#llaveshagarwal.com";
$subject = "New Email Subscriber";
$txt = "Name: ".$_POST['name']."\r\nFrom: ".$_POST['from'];
$headers = "From: ".$_POST['from'];
mail($to,$subject,$txt,$headers);
$to = $_POST['from'];
$subject = "Thank you for subscribing with us.";
$txt = "Hey ".$_POST['name']." ,\r\nGreetings from LLavesh Agarwal Textile Agency\r\nThank you for subscribing with us for Exclusive and Latest Catalogs and product launches.\r\nWe will update you soon.\r\n\r\nRegards,\r\nTeam LLavesh Agarwal\r\n\r\n*This is an automated Email. Please do not reply to this Email id. If you wish to talk to us, kindly Email us at hello#llaveshagarwal.com";
$headers = "From: notifications#llaveshagarwal.com";
mail($to,$subject,$txt,$headers);*/
$link = mysql_connect('localhost', 'llavesha_admin', 'Admin#1234');
$db= mysql_select_db('llavesha_contact_system', $link);
$insert='insert into subscribe (name,email) values ("'.$_POST["name"].'","'.$_POST["from"].'")';
if(mysql_query($insert)) {
echo "success";
} else {
echo "fail";
}
}
elseif($_POST['type'] == "contact") {
$to = "notifications#llaveshagarwal.com";
$subject = "New Contact";
$txt = "Name: ".$_POST['name']."\r\nContact: ".$_POST['mobile']."\r\nCategory: ".$_POST['bussiness_type']."\r\nDescription: ".$_POST['project_detail'];
$headers = "From: ".$_POST['from'];
mail($to,$subject,$txt,$headers);
$to = $_POST['from'];
$subject = "Thank you for contacting us.";
$txt = "Hey ".$_POST['name']." ,\r\nGreetings from LLavesh Agarwal Textile Agency\r\nThank you for contacting us.\r\nWe will update you soon !\r\n\r\nRegards,\r\nTeam LLavesh Agarwal\r\n\r\n*This is an automated Email. Please do not reply to this Email id. If you wish to talk to us, kindly Email us at hello#llaveshagarwal.com";
$headers = "From: notifications#llaveshagarwal.com";
mail($to,$subject,$txt,$headers);
echo "success";
}
?>
Are you working on windows (wamp)? Have you enabling php sendmail on php.ini? read: http://us3.php.net/manual/en/function.mail.php
or you can read: php mail() function on localhost
If you want to make it simpler, use phpmailer instead: https://github.com/PHPMailer/PHPMailer
How to use it by #pooria: How to configure PHP to send e-mail?
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender#gmail.com';
$mail->Password = '123!##';
$mail->SetFrom('me.sender#gmail.com', $name);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1#gmail.com', 'title1');
$mail->AddAddress('abc2#gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I trying to send an email from php using php mail() function.
Each time when I try to send an email it displays the error message.I don't know what went wrong.Here is my code. Can anyone tell me what went wrong ?
$message = "<html><body>";
$message .= "<table rules='all'>";
$message .= "<tr><td><strong>Name: Ramya</strong> </td></tr>";
$message .= "<tr><td><strong>Email: ramyaroy</strong> </td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = 'ramya#example.com';
$email='vinay#example.net';
$subject = 'Website Change Reqest';
$headers = "From:".$email.PHP_EOL;
$headers .= "Reply-To:".$email.PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1".PHP_EOL;
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
There's no error in the code.
The problem could be because of your hosting. Usually, hosting account will allow to send a mail from only that domain account.
Change the $email to vinay#[your_domain_name].com
Let me know after you try this.
<?php
/*
Below code works on live and local both server , also on SSL
which I describe all options on comments
you need to set SMTP server username and password ,
this is same as your google email id and password
*/
/* You need to download php-mailer class
https://github.com/PHPMailer/PHPMailer */
define('SMTP_USERNAME','your smtp username');
define('SMTP_PASSWORD','password');
define('FROM_EMAIL','from email');
define('ADMIN_EMAIL','replay to email');
define('SITE_NM','your site name');
print_r(sendEmail('john.doe#gmail.com','Lorem Ipsum','anything....'));
function sendEmail($to, $subject, $message) {
require_once("class.phpmailer.php");
// create a new object
$mail = new PHPMailer();
// enable SMTP
$mail->IsSMTP();
// debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPDebug = 0;
// authentication enabled
$mail->SMTPAuth = true;
//mail via gmail
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
//$mail->Host = "smtp.gmail.com";
//$mail->Port = 465; // or 587
//mail via hosting server
$mail->Host = SMTP_HOST;
if (SMTP_HOST == 'smtp.gmail.com') {
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 465; // or 58
}else{
$mail->Port = 587; // or 58
}
$mail->IsHTML(true);
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
//$mail->SetFrom(SMTP_USERNAME);
$mail->SetFrom(FROM_EMAIL);
$mail->AddReplyTo(ADMIN_EMAIL, SITE_NM);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($to);
// to add bcc mail list
$mail->AddBCC("example#gmail.com", "your name");
$mail->AddBCC("example2#gmail.com", "XXX XXXXX");
$result = true;
if (!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
$result = false;
}
return $result;
}
?>
$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
Thats the code. At the end it displays "Thank you for using our mail form", but i am not receiving any mail. Any ideas whats going wrong?
if(isset($_POST['Submit'])){
$to="email";
// this is your Email address
$from = $_POST['Email_Address']; // this is the sender's Email address
$first_name = $_POST['Full_Name'];
$tel_num=$_POST['Telephone_Number'];
$msg=$_POST['Your_Message'];
$subject = "Full Name : ".$first_name;
$headers = "From: ".$first_name." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$message="Full Name : ".$first_name."<br><br>Telephone Number : ".$tel_num."<br><br>Message : ".$msg;
$bericht = nl2br($message);
mail($to,$subject,$message,$headers);
}
You need to configure SMTP settings of your local server in php.ini file as follows:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Or you should give try to Swift Mailer or PHP Mailer
Try using fake sendmail to send emails in a windows enviroment.
http://jesin.tk/using-sendmail-on-windows/
Use php mailer() function
You can use PHPmailer :
http://phpmailer.codeworxtech.com/
Now
use following code -
<?php
require("class.phpmailer.php"); // give proper path of folder if needed
$mail = new PHPMailer();
session_start();
ob_start();
php?>
<your mail body goes here>
<?php
$body=ob_get_contents();
ob_end_clean ();
$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->IsHTML(true);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
You need to configure your mail SMTP in php.ini
It's quite simple, what is your ISP? For example, in comcast, just search in Google "comcast smtp address and port"
Take note of the SMTP address and Port, then go to your php.ini
Search for this configuration:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Change the address with the SMTP and smtp_port. Then you should be able to send emails in your localhost
I know my php form works here on godaddy server:
http://thespanishlanguageacademy.net/los-angeles/learn-spanish-kids-children/kontact.html
Please test it yourself put your email address in and it will send you a copy.
I copy the same code into a different server. This server is not go daddy. I know php works on this server, but for some reason this form is not working:
http://hancockcollege.us/kontact.html
Here is the php code:
// if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){
// put your email address here scott.langley.ngfa#statefarm.com, slangleys#yahoo.com
$youremail = 'bomandty#gmail.com';
// prepare a "pretty" version of the message
$body .= "Thank You for contacting us! We will get back with you soon.";
$body .= "\n";
$body .= "\n";
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
$body .= "\n";
}
$CCUser = $_POST['EmailAddress'];
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
$headers = "From: $_POST[EmailAddress]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'Form request', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
First,
Use this headers:
<?php
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
//Fom
$headers .= "From: XXX XXXX XXXXX <xxxx#xxxx.xxx>\r\n";
//Reply
$headers .= "Reply-To: example#example.com\r\n";
//Path
$headers .= "Return-path: example#example.com\r\n";
//CC
$headers .= "Cc: example#example.com\r\n";
//BBC
$headers .= "Bcc: example#example.com,example#example.com\r\n";
?>
Two, Read about PHPMailer and see the next code:
And try with this:
<?php
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//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 = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//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(file_get_contents('contents.html'), dirname(__FILE__));
//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.gif');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Maybe you have to initialize manually in the other server the SMTP path, i had a similar problem time ago, setting the correct SMTP fixed my problem.
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "yourmeail#yourdomain.com");
I am trying to send a filled up form to a particular email address. But I have no Idea about it. I know a little PHP coding and as far I have studied online I found that PHP already have a mail() function. but that also have some problems. although most of that I did not understood properly.
here is the things I want to do:
I want to send the filled up form to the particular email address.
I want to send that filled up form to my mobile phone so that i can reply immediately.
I want to send that mail to my inbox only (not spam pr junk).
I am requesting everyone to give me a details information about how I can do so.
thanks in advance..
You can simply use php mail function with all the parameters
for example
mail ( $to ,$subject , $message, $headers );
Where $to, $subject, $message are php variables to contains their respective values.
as you are posting data from the form so you can make $message from form.
like
$message = $_POST['FORM_FIELD_NAME'];
and $headers you set according to your requirement. For example
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Name <myemail#address.com>' . "\r\n";
it can help you..
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from#example.com";
$mail->AddAddress("id#example.net");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Use PHPMailer to send mails in PHP
http://phpmailer.worxware.com/
PHPMailer wiki page:
https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/UsefulTutorial
Use can use Gmail, Live or any other SMTP server to send mails
Code:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from#example.com";
$mail->AddAddress("myfriend#example.net");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>