New at PHP and trying to integrate mailgun to send to multiple forms. Below is the code for my PHP script which is using mailgun.
When sending the email using a simple mailgun script i am able to receive email but when i attach it to the forms i don't get any email.
require 'vendor/autoload.php';
use Mailgun\Mailgun;
else if($_GET['method'] == 'send_form'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email Subj for form 1';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
else if($_GET['method'] == 'send_form_two'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email subject for form 2';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
function send_mail($to, $from, $subject,$content)
{
$mgClient = new Mailgun('mailgunkey-here');
$domain = "mg.domain.com";
$result = $mgClient->sendMessage($domain, array(
'from' => $from,
'to' => $to,
'subject' => $subject,
'text' => $content
));
}
Seems like my code does not want 'from' => $from,
changing the from to static email resulted to sent mails.
Related
I want to send a email from php mail function. This is my code
<?php
$to = "modiv2301#gmail.com";
$subject = "HTML email";
$message = "
<h1>this is msg</h1>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <modiv2301#gmail.com>' . "\r\n";
$success=mail($to,$subject,$message,$headers);
if (!$success) {
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}else{
echo "Success";
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}
?>
when i run this code its showing me Success but i am not Receiving any email
My Php mail code is like this i want to send html content in mail but i can not send html template with this code
$to = $email;;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
// Sending email
if(mail($to, $subject, $message))
{
echo 'Your mail has been sent successfully.';
}
else
{
echo 'Unable to send email. Please try again.';
$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 = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$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.';
}
}
This kind of output i get in mail i do not know where is my mistake in this code..
Output
<html><body>
<table border="1" align="center" width="50%">
<tr><td>Test Mail</td></tr>
</table>
</body></html>
There are some errors in your code and also wrong if statement I would like to recommend following script to you for sending email.
$to = $email;
$subject = 'New Order Detail';
$message_text = 'Hi Jane';
$from = 'info#abc.co.in';
$from_name = 'John';
$to_name = 'Smith';
// Sending email
if(!empty($to) && !empty($message_text ))
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . ucwords($to_name) .' <'.$to.'>' . "\r\n";
$headers .= 'From: ' . ucwords($from_name) . ' <'. $from. '>' ."\r\n";
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
$message .= '<p style="color:#080;font-size:18px;">'.$message_text .'</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
echo 'Your mail has been sent successfully.';
}else{
echo 'Unable to send email. Please try again.';
}
add proper header contents.
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: abc <abc#example.com>, xyz <xyz#example.com>';
$headers[] = 'From: ABC <Abc#example.com>';
$headers[] = 'Cc: abc#example.com';
$headers[] = 'Bcc: xyz#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
Set your character set to UTF-8:
$headers .= "Content-type:text/ html;charset=UTF-8" . "\r\n";
That should fix it.
A variation on a theme perhaps and redundant as you have an answer. You should be able to add styles within the style tag region of the below code - or you could potentially add a link to an external css file but that could cause issues.
$to = $email;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
$obj=new StdClass;
$obj->success='Your mail has been sent successfully.';
$obj->fail='Unable to send email. Please try again.';
$obj->nl="\r\n";
$status = #mail( $to, $subject, $message );
if( $status ) {
exit( $obj->success );
} else {
$headers = array();
$html = array();
$headers[]="MIME-Version: 1.0";
$headers[]="From: {$from}";
$headers[]="Reply-To: {$from}";
$headers[]="X-Mailer: PHP/". phpversion();
$headers[]="MIME-Version: 1.0";
$headers[]="Content-type: text/html; charset=iso-8859-1";
$html[]="";
$html[]="<html>";
$html[]="<head>";
$html[]="<title>{$subject}</title>";
$html[]="<style>";
$html[]="p{color:red;font-size:18px;}";
$html[]="table{border:1px solid black;width:50%;display:table;}";
$html[]="td{text-align:center;background:yellow;color:blue;}";
$html[]="</style>";
$html[]="</head>";
$html[]="<body>";
$html[]="<table>";
$html[]="<tr><td>Test Mail</td></tr>";
$html[]="<tr><td><p>Will you marry me?</p></td></tr>";
$html[]="</table>";
$html[]="</body>";
$html[]="</html>";
$status = #mail( $to, $subject, implode( $obj->nl, $html ), implode( $obj->nl, $headers ) );
exit( $status ? $obj->success : $obj->fail );
}
I'm trying to send multiple e-mails with different messages.this is my code, it already works and could be sent on 'blazriku#gmail.com' and 'henrikus.antony#gmail.com' but the problem is "send message" and "welcome" always sent to both, not to each other.
then, how to make it "send message" to 'blazriku#gmail.com' and "welcome" to 'henrikus.antony#gmail.com?'
$email = 'blazriku#gmail.com , henrikus.antony#gmail.com';
$subject = array("send message", "Welcome");
$message = array("send message", "Welcome");
$name = array('Admin','Admin');
$to=$email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: kebunbibit.id <noreply#yourwebsite.com>'."\r\n" . 'Reply-To: '.$name.' <'.$email.'>'."\r\n";
$headers .= 'Cc: admin#yourdomain.com' . "\r\n"; //untuk cc lebih dari satu tinggal kasih koma
foreach (array_combine($subject, $message) as $subjects => $messages){
#mail($to, $subjects, $messages, $headers);
}
if(#mail)
{
print "<script>window.alert('E-Mail Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
else{
print "<script>window.alert('E-Mail Gagal Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
Rather than looping an email, you may create separate array of emails which are intended for sending different type of subject and emails.and afterwards loop those email array with specified subject and email content.see below:
$email = 'blazriku#gmail.com , henrikus.antony#gmail.com';
// $subject = array("send message", "Welcome");
// $message = array("send message", "Welcome");
$name = array('Admin','Admin');
$to=$email;
$welcome_msg_user_group = array('henrikus.antony#gmail.com');
$send_msg_user_group = array('blazriku#gmail.com');
$welcome_email = array('subject' => 'Welcome', 'msg_body' => 'blah blah');
$send_email = array('subject' => 'Send message', 'msg_body' => 'blah blah send msg body');
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: kebunbibit.id <noreply#yourwebsite.com>'."\r\n" . 'Reply-To: '.$name.' <'.$email.'>'."\r\n";
$headers .= 'Cc: admin#yourdomain.com' . "\r\n"; //untuk cc lebih dari satu tinggal kasih koma
foreach($welcome_msg_user_group as $welcome_user) {
#mail($welcome_user, $welcome_email['subject'], $welcome_email['msg_body'], $headers);
}
foreach($send_msg_user_group as $send_user) {
#mail($send_user, $send_email['subject'], $send_email['msg_body'], $headers);
}
if(#mail)
{
print "<script>window.alert('E-Mail Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
else {
print "<script>window.alert('E-Mail Gagal Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
Hope it helps you!
I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\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 = '<html><body>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}
Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$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";
// Additional headers
$headers .= 'To: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';
I have an html form the links to a PHP email. The form works well, but I am having trouble with the Cc and Bcc not coming through.
Here is the entire code. Please review and help me understand what I am getting wrong on the Cc and Bcc parts in the headers.
Thanks:
<?php
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
$email9_11 = $_POST['9-10'];
$email10_11 = $_POST['10-11'];
$email11_12 = $_POST['11-12'];
$email12_1 = $_POST['12-1'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} else {
$emailTo = "main#gmail.com" ;
$subject = "Family History Conference Registration";
if (!empty($emailFrom)) {
$headers = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$headers = 'From: Family History Conference <noreply#domain.org>' . "\r\n";
$headers .= 'Cc: $emailFrom' . "\r\n";
$headers .= 'Bcc: myemail#domain.com' . "\r\n";
}
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n\n";
$body .= "I would like to attend the following classes.\n";
$body .= "9:10 to 10:00: ".$email9_11."\n";
$body .= "10:10 to 11:00: ".$email10_11."\n";
$body .= "11:10 to 12:00: ".$email11_12."\n";
$body .= "12:10 to 1:00: ".$email12_1."\n";
/* Send Email */
if (mail($emailTo, $subject, $body, $headers)) {
echo "<h2>Thank you for Registering</h2>
<h3>You have registered for the following classes</h3>
<p>9:10 to 10:00am: \"$email9_11\" <br />
10:10 to 11:00am: \"$email10_11\"<br />
11:10 to 12:00: \"$email11_12\"<br />
12:10 to 1:00: \"$email12_1\"</p>
<p>We look forward to seeing you October 31, 2010</p>";
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>
You're using single quotes
$headers .= 'Cc: $emailFrom' . "\r\n";
PHP won't interpret variables inside single quotes, you must use double quotes
$headers .= "Cc: $emailFrom\r\n";