I would like to send an email to my gmail account using some simple PHP code. The code below works in terms of execution, however the problem is even thought is says "Message Sent" I am not receiving my email in my gmail account. Please advice
ini_set('SMTP',"smtp.gmail.com");
$to ="example#gmail.com"; // this will be replaced with my actual email
$from ="example#gmail.com"; // this will be replaced with senders email
$message = $_GET['Message'];
$subject = "This is a test";
if(mail($to,$subject,$message,$from))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
Steps to send a simple email
Go to google
Search for "PHP Mail"
Click the first result
Read, read, keep reading, wait, read it over, read on
Enjoy!
But seriously:
(Examples are taken from PHP.net)
Example 1
Sending a simple email
Using mail() to send a simple email:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated#example.com', 'My Subject', $message);
?>
Example 2
Sending mail with extra headers.
The addition of basic headers, telling the MUA the From and Reply-To addresses:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Use his line of code:
$to ="example#gmail.com"; // this will be replaced with my actual email
$from ="example#gmail.com"; // this will be replaced with senders email
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
//$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = $_GET['Message'];
$subject = "This is a test";
mail($to, $subject, $message, $headers);
Related
I tried sending HTML mails through PHP but nothing is working. I tried two options.
One with file_get_contents:
<?php
$to = 'teas#gmail.com';
$subject = 'Marriage Proposal';
$from = 'support#blabla.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = file_get_contents("email_template.html");
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
And one with HTML in a PHP string:
<?php
$to = 'anthony#gmail.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
The response for both functions is:
Unable to send email. Please try again. Unable to send email. Please
try again.
Can anyone tell me whats wrong?
Just enable the HTML in php mailer
$mail->isHTML(true);
For refrence please see here the example in github
i have a php script
<?php
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$message = 'mysitedomain.com';
$from = 'support#mysitedomain.com';
$headers = 'From:' . $from;
mail($to,$subject,$message,$headers);
echo 'Mail Sent.';
?>
When i run this code mail not send. If i change message to mysitedomaincom (without dot before com) the mail send succesfull.
Anybody have a solution for this?
This codes that tells the mailer and the recipient that the email contains well formed HTML that it will need to interpret
If you want you can change content of $message, now with this codes you can send HTML content mail.
<?PHP
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$headers = "From: Support <support#mysitedomain.com>" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>mysitedomain.com</h1>';
$message .= '</body></html>';
mail($to, $subject, $message, $headers);
?>
How can I send email notification to my users who gave registration details in my form using php?
I have code which runs perfectly to get emails to me, but now I also want to same details to my users.
I am trying to use "$from" into my array at "$to" but getting no email.
my mail.php
<?php
$subject = "Email Notification";
$message = "Thank you for your email";
$message = "Your Registering details are as follows:";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = $_POST['email'];
$to = array('my_address#example.com', 'my_address2#example.com', $from);
$lp = "notification#example.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$lp .'' . "\r\n" .
'Reply-To: '.$lp.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
It's pretty common that on shared hosting you need to send an email from an "existing email", at least match the domain. Maybe that's why the emails are not being sent?
So for example, if your domain is "www.my-awesome-domain.com", you can't send email headers
"from: example#example.com"
Instead, make sure to send emails from your domain, and ideally an existing email box, for example:
"from: office#my-awesome-domain.com"
Hope it helps! :)
I've got Mamp running on my mac and trying to get mail() to work.
This is what I've got to work with.
$to = 'mymail#gmail.com';
$subject = 'The subject!';
$message = 'Hi there!';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$headers .= 'From: Test <test#test.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers))
{ print 'success!'; }
else
{ print 'fail!'; }
?>
It just keeps on returning false. Any idea what I'm doing wrong?
Some settings with php/apache I need to check?
if you using your snippet on localhost, put on server and then try.
php mail() function needs to be on sever if you want it to work. on localhost you always get fail!
Try this:
<?php
$Name = "Da Duder"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
ini_set('sendmail_from', 'me#domain.com');
mail($recipient, $subject, $mail_body, $header);
?>
http://be.php.net/manual/en/function.mail.php
each line of text may not be bigger than 70 chars and needs to be cut off with a LF (\n)
EDIT: as #brad suggested: SwiftMailer is realy good!
<?php
$sendto = "account#gmail.com";
$subject = "email confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
# send the email
mail($sendto, $subject, $message);
?>
this is the code that i wrote to test mail function on localhost.
i have ran the script in browser for several times and still dun receive any email in my mail box.
Do I need any additional configurations?
thx in advance!
Basically is hard to send a mail from localhost to any mail providers.
They have big restrictions on the incoming mails, and the simply mail() won't work.
You need to use an SMTP server.
and define that server in php configuration
smtp = localhost #(here should be your smtp server)
smtp_port = 25
if you don't have an SMTP server, try to pass all headers like in PHP examples:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
http://www.php.net/manual/en/function.mail.php
You need to make sure that you have your PHP installation set up to use a working SMTP Server. You might find what you're looking for in answers to this question. Failing that you'll likely need to test your script on your live web server.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'yourname#yourwebsite.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message"
?>
<?php
$to = "inspiretechpark#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$headers .= "Cc: someone#domain.com \r\n";
$headers .= "Bcc: someoneelse#domain.com \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
Try This Guys..This Is For Sending Mail
Try this:
<?php
$sender = 'email#example.com';
$recipient = 'email#example.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 working with localhost then, i hope it will never work. It will work only on a mail configured server. Please try on it.