How to send email using PHP? - php

I want to send email using PHP when an HTML form is submitted.
I learnt from a tutorial and made this PHP script, but when I check it in my webmail, I only see email address, subject and message, but no name. What do I need to do to get the name to show up?
Form variables:
if (empty($_POST) === false) {
$errors = array();
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
// ... etc (validation)
if (empty($errors) === true) {
$headers = 'From: '.$email. "\r\n" . $name.
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('mail#example.com',$subject,$message,$headers);
print "<p class='formerrors'>Thank you for your message, I'll get back to you shortly!</p>";
}

The format of your "From" header isn't right. It should be in the following Format:
"From: Sender Name <sender#domain.com>"
So your $headers assignment should read:
$headers = 'From: '.$name.' <'.$email. ">\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();

You should modify your $headers variable as:
$headers = 'From: ' .$name. ' <' .$email. '>' . "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Please read more about mail function on php.net over here: http://php.net/manual/en/ref.mail.php

Moreover maybe PHPMailer, see https://github.com/PHPMailer/PHPMailer is interesting for you to enrich your mails.

Related

Unable to send emails while i pass headers to mail() function

I am trying to send emails with HTML content and whenever i pass headers of any kind the method mail() return false but with no headers i get true .. any clue ?
$to = $user->email;
$name = $user->first_name.' '.$user->last_name;
$from = $this->config->item('admin_email', 'ion_auth');
$subject = $this->config->item('site_title', 'ion_auth') . ' - ' . $this->lang->line('email_forgotten_password_subject');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval = mail ($to,$subject,$message,$headers);
die(var_dump($retval));
Make sure that variables $name, $from, $to, $subject has the right data and are not empty/null. Your code is functional and should send the email.
If still doesn't work, check the configurations on postfix or what other mail server you're using.
And I suggest to use PhpMailer or SwiftMailer instead of simple mail function.
Try adding a CRLF after your last header. Used to work for me in the olden days.
i.e.,
...
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n";
...
Also, the header Content-Type has a capital T. Sometimes, such trivial things may also break it. That's why everyone suggests to move away from it.
That should do it.
Suggestion: Switch to PHPMailer.

php: Can't send email

I've made a website with a mailing form, and the php to send it is as follows:
<?php
$to = "example#outlook.com";
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
It won't send anything.
Can anyone tell what's wrong in the script?
If it helps on anything, I'm trying to send to an Outlook email.
Thanks

Add space between two variables in the phpmailer headers

I'm sending a form mail with php.
I need to set the "from:" to the form fields which live in the variables:
$user_name and $user_lastname.
So i tried:
$headers .= 'From: ' . $user_name . " " . $user_lastname . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
But then i get "unknown sender" instead of name and last name.
try this:
$headers .= 'From: "'.$user_name." ".$user_lastname.'"<'.$user_email.'>'."\r\n";
I believe you're simply using the wrong (").
Try this:
$headers .= 'From: '. $user_name .' '. $user_lastname;
$headers .= 'X-Mailer: PHP/' . phpversion();
Let me know if it isn't working

how to make php code to email login and password to emails in mysql database

I have some affiliate script that I buy, however this script doesn't have function to
"Send password and login id to all my affiliate" weekly or maybe once per 2 week.
This is because in my experience he/she do not have many members not coming back because not only forgot password but also forgot username.
I want every month the members get email example
To: cubaan04#hotmail.com
From : admin#admin.com
Subject : Reminder : Your login name and password for website
Hi Firstname,
We worry if you forgot your login name and password. We love to see you back and active in our network. Here the detail...
your login name : Cubaan04
your password : Cubaan04
please login to website
Your truely
admin website
I found this script but dont know how to modified it
<?php
$db = mysql_connect("localhost", "plazacen_t", "plazacentrey");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT `email` FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0]);
}
mysql_free_result($result);
function sendMail($to){
$subject = 'the subject';
$message = 'hello Shahrul';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
it not work
and how to put password and login in massage from the database?
Thanks you for read this, iam just noob for this
hope get help from you guys
updated
From our friend answer, i modified it. Yes it send email now but not work like i want, i put
<?php
$db = mysql_connect("localhost", "plazacen_rak", "rakan852");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT email,password,name FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:". $id ." password: ". $pwd ."';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
what I received is email like this;
From : webmaster#example.com
the subject
04/04/2015 9.59 PM
your login name:". $id ." password: ". $pwd ."
it is not change the $id to username in database, so do the $pwd
don't know whats wrong, any further help. TQ
The problem is with your sendMail function, You cannot take variables inside single quotes modify it as
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = "your login name: {$id} password: {$pwd}";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
another alternative could be
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:' . $id .' password: '. $pwd;
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Both will work like a charm.
First change your query to:
$result = mysql_query("SELECT email,password,userid FROM `affiliate_users`");
Then change your code like this:
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
**$message = "your login name:". $id ." password: ". $pwd ."";**
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Note
I do not know what your field names are in the affiliate_users table so replace this "email,password,userid" with your actual field name.
If you want to execute this script every month then you can use cronjob.

PHP / Wordpress sending email to multiple addresses

I am trying this on wordpress, trying to email multiple email address. I have 2 email addresses in the database.
Here is the code:
$r = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_users", $ID, $user_email ));
foreach ($r as $row) {
$to = 'someone#myhost.com';
$bcc = $row->user_email;
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo "Email sent";
}
else {
echo "Email sending failed";
}
It's sending emails BUT what's happening is that the TO (someone#myhost.com) is getting 2 emails and the $bcc is getting none.
What am I doing wrong here?
Yep, this behavior it is pretty normal, you forgot to put in $headers the Bcc: part, it should be like:
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Bcc: '.$bcc. "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Categories