PHP - How To Send Mail With Name And Email?
Hi guys,
I have a problem with the $header variable. I am trying to extract the value from the $name method and included with the $email method. <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypass'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $name = clean($_GET['name']); $email = clean($_GET['email']); $type = clean($_GET['type']); $comments = clean($_GET['comments']); if($name == ''){ $errmsg_arr[] = 'name are missing.'; $errflag = true; } elseif($email == ''){ $errmsg_arr[] = 'email are missing.'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['name'])) { $insert[] = 'name = \'' . clean($_GET['name']) .'\''; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['type'])) { $insert[] = 'type = \'' . clean($_GET['type']) . '\''; } if(isset($_GET['comments'])) { $insert[] = 'comments = \'' . clean($_GET['comments']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($email)){ $name = $_GET['name']; $email = $_GET['email']; $header = 'From: $name <-f $email>' . "\r\n"; $to = "myname@myemail.com"; $subject = $type; $message = "$comments"; $header .= "MIME-Version: 1.0\l\n"; mail($to, $subject, $message, $header); echo "Thank you for sent us your email"; } } } ?> Something got to do with this line: $header = 'From: $name <-f $email>' . "\r\n"; My problem is I can see in my email that the sender name has been included with (unknown sender) while the email address are display as empty address. I am really not sure why I have got the blank sender name and with the blank sender address. Do you know what the main problem is and what I need to change it with? Any advice would be much appreciate. Thanks, Mark Similar TutorialsHello Im making html mail for my project. However the mail() function of PHP does not send my email. Code: [Select] <?php $message = ' <html> <head> <title>Test Email</title> </head> <body> <h4>This is a test email message from me</h4> <br/> <p><b>Lorem Ipsum<b><br/> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </body> </html> '; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'To: me <darkstarnexus@yahoo.com>' . "\r\n"; $headers .= 'From: Sample <sample@mail.com>' . "\r\n"; $headers .= 'Cc: Sample2 <sample2@mail>' . "\r\n"; $headers .= 'Bcc: sample3@mail' . "\r\n"; if(mail('darkstarnexus@yahoo.com', 'Test Html Email', $message, $headers)){ echo "mail delivered"; }else{echo "mail is not delivered";} ?> Can some tell me why? Hello, I have some troubles to send mass email. I am using mail() function and the script is timed out. also there is a restriction in which you allowed to send 20 mails per minute. so I use sleep(5) to delay 5 seconds between every mail, but the script is exceeded timeout. Adding this: set_time_limit(0); will still not work. maybe can I set a background thread? if so please tell me how to. Also, if there is a built in script that can help me with that, I would like to hear about it. Thanks =] When I test my php, all of the errors work, but when I actually want to send an email the form doesnt go through and I get the error message 'Please enter a valid e-mail address'? I think I may have something wrong in my mail function. Code: [Select] <?php if (empty($_POST['author'])) { $errors[] = 'Please enter a name'; } if (empty($_POST['subject'])) { $errors[] = 'Please enter a subject'; } if (empty($_POST['text'])) { $errors[] = 'Please enter your comments.'; } if (empty($_POST['email'])) { $errors[] = 'Please enter an e-mail'; } else if (!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ ^", $_POST['email'])) { $errors[] = 'Please enter a valid e-mail address'; } if (count($errors) == 0) { // Process form mail("solysol05@gmail.com","Subject: subject", text,"From: author <email>"); echo "Thank you for using our mail form.<br/>"; echo "Your email has been sent."; } else { echo $errors[0]; } ?> Code: [Select] <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $dropdown = $_POST['dropdown']; $formcontent=" From: $firstname \n Surname: $lastname \n Email: $email \n Dropdown: $dropdown"; $recipient = "martin@sittingspiritually.co.uk, siobhan@sittingspiritually.co.uk"; $subject = "Newsletter Sign Up"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); if ( mail($recipient, $subject, $formcontent, $mailheader) ){ header('Location: http://www.sittingspiritually.co.uk/thankyou.php'); } else { die ("error"); } ?> I have recently created a small snippet of php code for posting mail and it's sweet apart from 1 thing. I have two email addresses in the recipient section. It only sends to the last name in the list. Is this because i have written recipient? Should it be recipients? I need this mail to be delivered to both of the email addresses. I'm sure it's simple for someone with expert knowledge. Thanks in advance for any help. This topic has been moved to Other Web Server Software. http://www.phpfreaks.com/forums/index.php?topic=347009.0 Hello all, I used simple php email function but it send an email in junk folder or spam. Can anyone tell me why is this so? Her is the code: $host = "ssl://smtp.gmail.com"; $port = "465"; $to = " xx@gmail.com"; // note the comma $subject = " $_POST[company_website] "; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: About Wholesale Account' . "<$_POST[email]>\r\n"; $headers .= 'Cc: mail@gmail.com' . "\r\n"; $headers .= 'Bcc: cc@gmail.com' . "\r\n"; mail($to, $subject, $message, $headers); Everything about the email is sending except the message text does anyone know what the issue could be? here is the block of code that sends the email Thanks in advance Code: [Select] $image = "http://www.visualrealityink.com/dev/clients/arzan/snell_form/images/email.png"; echo "got to process form"; $target_path = "upload/"; $path = $target_path = $target_path . basename( $_FILES['file']['name']); $boundary = '-----=' . md5( uniqid ( rand() ) ); $message .= "Content-Type: application/msword; name=\"my attachment\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment; filename=\"$path\"\n\n"; echo $path; $fp = fopen($path, 'r'); do //we loop until there is no data left { $data = fread($fp, 8192); if (strlen($data) == 0) break; $content .= $data; } while (true); $content_encode = chunk_split(base64_encode($content)); $message .= $content_encode . "\n"; $message .= "--" . $boundary . "\n"; $message .= $image . "<br />" . $_POST['name'] . "submitted a resume on our website. Please review the applications and contact the candidate if their resume is a fit for any open opportunities with the company. <br><br> Thank you. <br><br> SEI Team"; $headers = "From: \"Me\"<me@example.com>\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\""; mail('george@visualrealityink.com', 'Email with attachment from PHP', $headers, $message); Hello Guys,
I need your help as many PHPers here is more experienced in PHP coding than me. I have specific project I am working on and need a piece of code that can send an email from HTML form using PHP to the email address that is entered manually on the form, instead of standard sent to PHP code that is fixed within PHP script and executed during submission. I want sth that can grab manually enetered recipient's e-mail address, paste it to the PHP code and then use it to send the email to the recipient, instead of fixed sent to code. Something would say dynamically changed during the entry that can inject into PHP code the new address email entered on the form and then submit to it. Any ideas will be great.
Thanks.
Edited March 27, 2019 by slawotrend Can somebody tell me how to make this exact code send mail? I tested it on my website and it doesn't send any emails. The first code is the file contact_config.php <?php $mailto = "youremail@email.com"; $charset = "windows-1251"; $subject = "Site visitor: ".$_POST['posName']; $content = "text/html"; $message = "Site visitor information: <br><br> Name: ".$_POST['posName'] ."<br>E-mail: ".$_POST['posEmail'] ."<br>Country: ".$_POST['posCountry'] ."<br>Phone: ".$_POST['posRegard'] ."<br>Comments: ".$_POST['posText']; $statusError = ""; $statusSuccess = ""; $errors_name = 'Please enter the Name'; $errors_mailfrom = 'Please enter the Email'; $errors_incorrect = 'The e-mail address you entered does not eppear to be valid. <br>Your e-mail address should look like yourname@domain.com'; $errors_message = 'Please enter the Message'; $errors_subject = 'Please enter the Phone'; $captcha_error = 'Wrong security code!'; $send = 'Thank you for your message'; ?> <?php $mailto = "youremail@email.com"; $charset = "windows-1251"; $subject = "Site visitor: ".$_POST['posName']; $content = "text/html"; $message = "Site visitor information: <br><br> First Name: ".$_POST['posName'] ."<br>Last Name: ".$_POST['posName2'] ."<br>E-mail: ".$_POST['posEmail'] ."<br>Telephone: ".$_POST['posRegard'] ."<br>City where jobsite is located: ".$_POST['posText'] ."<br>want us to e-mail you the free Homeowners Guide To Remodeling: ".$_POST['posBox']; $statusError = ""; $statusSuccess = ""; $errors_name = 'Please enter the First Name'; $errors_name2 = 'Please enter the Last Name'; $errors_telephone = 'Please enter the Telephone'; $errors_city = 'Please enter the City where jobsite is located'; $errors_mailfrom = 'Please enter the Email'; $errors_incorrect = 'The e-mail address you entered does not eppear to be valid. <br>Your e-mail address should look like yourname@domain.com'; $captcha_error = 'Wrong security code!'; $send = 'Thank you for your message'; ?> The second code is contacts.php <?php include('kcaptcha/kcaptcha.php'); session_start(); require_once("contact_config.php"); if ($_POST['act']== "y") { if(isset($_SESSION['captcha_keystring']) && $_SESSION['captcha_keystring'] == $_POST['keystring']) { if (isset($_POST['posName']) && $_POST['posName'] == "") { $statusError = "$errors_name"; } elseif (isset($_POST['posEmail']) && $_POST['posEmail'] == "") { $statusError = "$errors_mailfrom"; } elseif(isset($_POST['posEmail']) && !preg_match("/^([a-z,._,0-9])+@([a-z,._,0-9])+(.([a-z])+)+$/", $_POST['posEmail'])) { $statusError = "$errors_incorrect"; unset($_POST['posEmail']); } elseif (isset($_POST['posText']) && $_POST['posText'] == "") { $statusError = "$errors_message"; } elseif (!empty($_POST)) { $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: $content charset=$charset\r\n"; $headers .= "Date: ".date("Y-m-d (H:i:s)",time())."\r\n"; $headers .= "From: \"".$_POST['posName']; $headers .= "X-Mailer: My Send E-mail\r\n"; mail("$mailto","$subject","$message","$headers"); $_POST['posRegard'] = ""; $_POST['posText'] = ""; $_POST['posCountry'] = ""; $_POST['posEmail'] = ""; $_POST['posName'] = ""; unset($name, $posText, $mailto, $subject, $posRegard, $message); $statusSuccess = "$send"; } }else{ $statusError = "$captcha_error"; unset($_SESSION['captcha_keystring']); } } $cat_name="Contact $store_name manager"; ?> Now for some reason the two of these files are supposed to send me emails when customers click the submit email button on my form but, I don't get any emails. I tested it myself and no emails. Help please! Hello, I have been using the mail() command for a while now, but for some reason It has just stopped functioning all together. I have added an OR DIE rule to the end of it and had no response. To all intents and purposes that mail is being sent - there are no errors whatsoever - just the mail is not getting delivered. I cannot think of anything I've done or changed that would stop it from being delivered, I haven't even been near the function that does it in a while. Here is the code: Code: [Select] function sendMail($to,$ref) { $subject = 'Booking Confirmation'; $file = "receipts/$ref.htm"; $fh=fopen($file, "r"); $message = fread($fh, filesize($file)); fclose($fh); $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Company <noreply@company.co.uk>' . "\r\n"; mail($to, $subject, $message, $headers); } I have taken out the public names involved, but otherwise thats the code. It has worked absolutely fine in the past, and even If I strip out all of the filehandling part of it, and just use a simple $from - $to - $message, it still seems not actually send the mail. It wouldnt be so bad If I at least had an error to go on, but I dont I am just simply not getting the mail. Please help! ~Chud37 Hi People,
I'm using a php to send an mail using a server via ssl. My program:
<?php I'm trying to send email with php for the first time. Trying to make it so that after a user registers he/she receives an email after submitting. I'm trying to send it from 1 comp with an email of goldie@telkomsa.net to another with an email of jgoldie@telkomsa.net (I just really want to test if it's sending email another before I get technical with the email) Should this code work, here is part of my code with the email section in it: Code: [Select] <?php // query $sql = "INSERT INTO student (sno, sname, init, fname, title, msname, dob, sex, lang, idno, telh, telw, cel, fax, email, address, contact_flag ) VALUES ('', '$sname', '$init', '$fname', '$title', '$msname', '$dob', '$sex','$lang', '$idno', '$telh', '$telw', '$$cell', '$fax', '$email', '$address', '$contact')"; mysql_query($sql) or die('Error:' . mysql_error()); $sno_id = mysql_insert_id(); // get the cid $cname = mysql_real_escape_string($_POST['cname']); $getCID = "SELECT cid FROM course WHERE cname='$cname'"; $result = mysql_query($getCID); if($result) { $row = mysql_fetch_assoc($result); $course_id = $row['cid']; // add the student to the course_student table $addCID = 'INSERT INTO course_student (cid, sno) VALUES(' . $row['cid'] . ', ' . $sno_id . ')'; mysql_query($addCID) or die('Error:' . mysql_error()); // send email $Name = "Da Duder"; //senders name $email = "goldie@telkomsa.net"; //senders e-mail adress $recipient = "jgoldie@telkomsa.net"; //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', 'goldie@telkomsa.net'); //Suggested by "Some Guy" if (mail($recipient, $subject, $mail_body, $header)){ echo "mail has been sent"; //mail command } } header( "refresh:5;student_man.php" ); echo 'Registration <b> successful </b> You\'ll be redirected in about 5 secs. If not, click <a href="student_man.php">here</a>.'; } } ?> Email is under the "email section" comment based of what I found on google. I'm not getting any errors so I want to know if the email isn't going through because of my code or my smtp setting etc. Hello, I have those code: $message = " Hello $inforow2['company']<br /> This is a notice about a pending invoice at $inforow3['company'].<br /> If you would like to see the invoice, you can see it <a href='http://$inforow3['url]'/view-invoice?id=$id.php'>here</a><br /> This is an automatic message. Replies will not be monitored.<br /> Yours faithfully<br />$inforow3['company']"; mail($inforow2['email'],"Invoice overdue #1",$message,$inforow3['sentfrom']); I'm not sure if I can send this, and it will get the values from my mysql and put it in the email or it would send the text directly, and if it ill take the html.. Can any one tell me if above code would be correct? I understand you need to use the mail() command but this just wont work for me, my webserver is with godaddy, and they sent me an email saying that i need to use a relay server to send my mail, how do i set this up or anything? this worked and stopped, no error no idea why is this a conflict? Code: [Select] echo("<meta http-equiv = refresh content=0;url=".$url_success.">"); mail($to_supplier, $subject_supplier, $message_supplier, $headers_supplier); $ok = @mail($email_to, $email_subject, $email_message, $headers); Hi guys, I'm sending mail from php running on a cheap webhost with the following: Code: [Select] $to = 'name@domain.com.au'; $header = 'MIME-Version: 1.0' . "\r\n"; $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $header .= "From: Name <noreply@domain.info>" . "\r\n"; $subject = "Interest registered"; $message = "<!DOCTYPE HTML.. a bunch of html"; mail($to, $subject, $message, $header); As you can see I've set "from" in the headers. When I receive the email however, it says from: myusername@my.cheaphost.com on behalf of Name [noreply@domain.info] I need to get rid of the on behalf bit as it looks completely unprofessional. I just want it to say from: Name [noreply@domain.info] Obviously this is probably something to do with the setup of the server, some of which I have no control over. I have access to another mail server which I can use to send email, so I put this at the start of my code: Code: [Select] ini_set('SMTP', 'mail.myotherserver.com.au'); echo ini_get('SMTP'); Which returns mail.myotherserver.com.au however, when I use the mail function, it still comes from myusername@my.cheaphost.com on behalf of Name [noreply@domain.info] Is there something I'm missing, is the mail function not tied to the SMTP value? Thanks Hey my name is Brandon, im quite new to php but im improving and I really want to get into web design soon. Well lucky for you guys I was bored so i decided to create a Contact Us page. Just so you know MY contact us requires the user to be logged in to use it otherwise it redirects them to the login.php page and outputs a error saying "you need to be logged in to access this page", however i changed this contact us page so the user does not have to be logged in and it just requires all the fields to be entered. One thing I diden't add was a system to check if the email has a "@" sign in it, so you can be more confident that the email is real, I don't find this that important because if the user is leaving out a "@" sign then it's likely they are just wanting to send a fake email and they will do it regardless of a check or not. Here's The Code. <?php function mysql_prep($value) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists("mysql_real_escape_string"); // i.e PHP >= v4.3.0 if($new_enough_php){ // PHP v4.3.0 or higher if ($magic_quotes_active){ $value = stripslashes($value); } $value = mysql_real_escape_string($value); }else{ //Before PHP v4.3.0 //if magic quotes aren't already on then add slahes manually if(!$magic_quotes_active){ $value = addslashes($value); } // if magic quotes are active then the slashes already exist } return $value; } function redirect_to($location = NULL){ if($location != NULL){ header("Location: {$location}"); exit; } } ?> <?php if (isset($_POST['submit'])){ // The form has been submitted $errors = array(); // Perform validations on the form $required_fields = array('title', 'email', 'message'); foreach($required_fields as $fieldname){ if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){ $errors[] = $fieldname; } } $field_with_lengths = array('title' => 20, 'email' => 50); foreach($field_with_lengths as $fieldname => $maxlength) { if (strlen(trim($_POST[$fieldname])) > $maxlength) { $errors[] = $fieldname; } } // Set the needed variables here $email = $_POST['email']; $title = $_POST['title']; $message = $_POST['message']; if (empty($errors)){ // Their are no errors in the form $headers = 'From: ' . $email . "\r\n" . 'Reply-To: bcooperz@hotmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $sentmail=mail("bcooperz@hotmail.com","$title","$message", "$headers"); // if your email successfully sent if($sentmail){ echo "Your Email Has been sent"; }else{ echo "Cannot send email"; } }else{ $count = count($errors); if($count == 1){ echo "Their Was {$count} Error In The Form" . "<br />"; echo "<b>"; print_r(implode(", ", $errors)); echo "</b>"; }else{ echo "Their Was {$count} Error's In The Form" . "<br />"; echo "<b>"; print_r(implode(", ", $errors)); echo "</b>"; } } }else{ // The Form Has Not Been Submitted $email = ""; $title = ""; $message = ""; } ?> <html> <head> <title>Contact Us</title> </head> <body> <form action="contact.php" method="post"> Title of message : <input type="text" name="title" maxlength="20" value="" /><br /> Your Email : <input type="text" name="email" maxlength="50" value="" /><br /> Message : <br /><textarea name="message" rows="20" cols="80"></textarea><br /><br /> <input type="submit" name="submit" value="Contact Us" /><br /> </form> </body> </html> Now in order for this to work you will need to have a domain or localhost will also do fine if you have it set up with a email system, some free domains will work as long as they have a email sending capability and php enabled. Also one thing you will want to do is change "bcooperz@hotmail.com" in that code to your email so the users of your website are sending the emails to your email Thanks, BcooperZ. hello i`m using this script found on google to send a mail with attachement <?php $strTo = "roccilaura@libero.it"; $strSubject = "Test sending mail."; $strMessage = "My Body & <b>My Description</b>"; //*** Uniqid Session ***// $strSid = md5(uniqid(time())); $strHeader = ""; $strHeader .= "From: Mr.Weerachai Nukitram<email@email.com>\n"; $strHeader .= "Cc: Mr.Surachai Sirisart<email@email.com>\n"; $strHeader .= "Bcc: webmaser@email.com"; $strHeader .= "MIME-Version: 1.0\n"; $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n"; $strHeader .= "This is a multi-part message in MIME format.\n"; $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 // $strHeader .= "Content-Transfer-Encoding: 7bit\n\n"; $strHeader .= $strMessage."\n\n"; //*** Files 1 ***// $strFilesName1 = "attach.html"; $strContent1 = chunk_split(base64_encode(file_get_contents($strFilesName1))); $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName1."\"\n"; $strHeader .= "Content-Transfer-Encoding: base64\n"; $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName1."\"\n\n"; $strHeader .= $strContent1."\n\n"; $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error // if($flgSend) echo "Mail send completed."; else echo "Cannot send mail."; ?> its sends good but one question how can i insert into the attach file values like $name $address directly from a text file |