james_byrne Posted July 8, 2008 Share Posted July 8, 2008 I am trying to use vcalendar on a site that i am working on and i need to edit the e-mail function they are using to put a user name and password in for the smtp server. My question is how can i pass the a user name and password for my smtp server to the php mail function so that it can use my smtp server as a relay. Thanks in advance for the help. Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/ Share on other sites More sharing options...
jonsjava Posted July 8, 2008 Share Posted July 8, 2008 stolen from About.com <?php require_once "Mail.php"; $from = "Sandra Sender <[email protected]>"; $to = "Ramona Recipient <[email protected]>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "mail.example.com"; $username = "smtp_username"; $password = "smtp_password"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?> Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584438 Share on other sites More sharing options...
DeanWhitehouse Posted July 8, 2008 Share Posted July 8, 2008 or use the PHPmailer google it. Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584442 Share on other sites More sharing options...
james_byrne Posted July 8, 2008 Author Share Posted July 8, 2008 I have tired this and i am getting a error. I am using this in a function. error: Warning: Cannot modify header information - headers already sent by (output started at /var/www/vcalendar/common_functions.php:316) in /var/www/vcalendar/registration.php on line 532 here is there original code: function SendEmailMessage($variable, $email_to, $parameters) { global $calendar_config; $value = 0; if (strlen($variable)) { $db = new clsDBcalendar(); $SQL = "SELECT email_template_from, ". "email_templates.email_template_subject AS mainSubject, ". "email_templates_lang.email_template_subject AS localeSubject, ". "email_templates.email_template_body AS mainBody, ". "email_templates_lang.email_template_body AS localeBody ". "FROM email_templates LEFT JOIN email_templates_lang ". "ON email_templates.email_template_id = email_templates_lang.email_template_id ". "WHERE email_template_type = ".$db->ToSQL($variable, ccsText). " AND language_id = ".$db->ToSQL(CCGetSession("locale"), ccsText); $db->query($SQL); if ($db->next_record()) { $email_subject = CCStrLen($db->f("localeSubject"))? $db->f("localeSubject") : $db->f("mainSubject"); $email_body = CCStrLen($db->f("localeBody"))? $db->f("localeBody") : $db->f("mainBody"); $email_from = CCStrLen($db->f("email_template_from"))? $db->f("email_template_from") : $calendar_config["site_email"$ while (list($key,$value) = each($parameters)) $email_body = str_replace($key,$value,$email_body); $email_body = str_replace("{site_url}",ServerURL,$email_body); if (strlen(trim($calendar_config["SMTP"]))) { $value = mailsmtp($email_from, $email_to, $email_subject, $email_body, "From: $email_from\nContent-Type: tex$ } else { $value = @mail($email_to, $email_subject, $email_body, "From: $email_from\nContent-Type: text/plain"); if ($value) $value = ""; else $value = "Email sending ERROR"; } } $db->close(); } return $value; } and when i change it it looks like this: function SendEmailMessage($variable, $email_to, $parameters) { global $calendar_config; $value = 0; if (strlen($variable)) { $db = new clsDBcalendar(); $SQL = "SELECT email_template_from, ". "email_templates.email_template_subject AS mainSubject, ". "email_templates_lang.email_template_subject AS localeSubject, ". "email_templates.email_template_body AS mainBody, ". "email_templates_lang.email_template_body AS localeBody ". "FROM email_templates LEFT JOIN email_templates_lang ". "ON email_templates.email_template_id = email_templates_lang.email_template_id ". "WHERE email_template_type = ".$db->ToSQL($variable, ccsText). " AND language_id = ".$db->ToSQL(CCGetSession("locale"), ccsText); $db->query($SQL); if ($db->next_record()) { $email_subject = CCStrLen($db->f("localeSubject"))? $db->f("localeSubject") : $db->f("mainSubject"); $email_body = CCStrLen($db->f("localeBody"))? $db->f("localeBody") : $db->f("mainBody"); $email_from = CCStrLen($db->f("email_template_from"))? $db->f("email_template_from") : $calendar_config["site_email"$ while (list($key,$value) = each($parameters)) $email_body = str_replace($key,$value,$email_body); $email_body = str_replace("{site_url}",ServerURL,$email_body); require_once "Mail.php"; $from = $email_from; $to = $email_to; $subject = $email_subject; $body = $email_body; $host = "mysmtpserver"; $username = "XXXXXX"; $password = "XXXXXXXXXX"; $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array('host' =>$host, 'auth' => $ture, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); $vaule = ""; } else { echo("<p>Message successfully sent!</p>"); $value = "Email sending ERROR"; } } $db->close(); } return $value; } if i run a smiler script in its own file it works fine, but it will not work it in a function i guess. Is there anyone who has any ideas. Thanks in advance for the help. Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584579 Share on other sites More sharing options...
DeanWhitehouse Posted July 8, 2008 Share Posted July 8, 2008 the header must be before any html output(i think). Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584620 Share on other sites More sharing options...
james_byrne Posted July 8, 2008 Author Share Posted July 8, 2008 Ya you are correct, so i made it so it at least tells me that it is sending the e-mail now. The only thing is that it is not sending anything at all. Something else to troubleshoot. But thanks to everyone for the help. I appreciate it. Link to comment https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.